Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. copy constructor

copy constructor

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
8 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    packetlos
    wrote on last edited by
    #1

    Hi there, If I add a CArray such as CByteArray to a struct like so: typedef struct DATAGRAM { // other types here... CByteArray pkt_data; // Packet data } DATAGRAM; and then try and assign something of type DATAGRAM to type DATAGRAM i get the following compile error: C2558: struct 'DATAGRAM' : no copy constructor available or copy constructor is declared 'explicit' I know this is because because CByteArray is of dynamic type so I need to provide a copy constructor. So my question is do I need to provide one for DATAGRAM or do I really need to provide one for CByteArray and if so could somebody post a code snippet to help me out? Cheers Packetlos

    T A J P 4 Replies Last reply
    0
    • P packetlos

      Hi there, If I add a CArray such as CByteArray to a struct like so: typedef struct DATAGRAM { // other types here... CByteArray pkt_data; // Packet data } DATAGRAM; and then try and assign something of type DATAGRAM to type DATAGRAM i get the following compile error: C2558: struct 'DATAGRAM' : no copy constructor available or copy constructor is declared 'explicit' I know this is because because CByteArray is of dynamic type so I need to provide a copy constructor. So my question is do I need to provide one for DATAGRAM or do I really need to provide one for CByteArray and if so could somebody post a code snippet to help me out? Cheers Packetlos

      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #2

      isn't it because you use a struct instead of a class ? i know C++ enlarge structures capabilities, but i wonder if that's possible...


      TOXCCT >>> GEII power

      1 Reply Last reply
      0
      • P packetlos

        Hi there, If I add a CArray such as CByteArray to a struct like so: typedef struct DATAGRAM { // other types here... CByteArray pkt_data; // Packet data } DATAGRAM; and then try and assign something of type DATAGRAM to type DATAGRAM i get the following compile error: C2558: struct 'DATAGRAM' : no copy constructor available or copy constructor is declared 'explicit' I know this is because because CByteArray is of dynamic type so I need to provide a copy constructor. So my question is do I need to provide one for DATAGRAM or do I really need to provide one for CByteArray and if so could somebody post a code snippet to help me out? Cheers Packetlos

        A Offline
        A Offline
        antlers
        wrote on last edited by
        #3

        You don't need to provide one for CByteArray if you provide one for DATAGRAM (that does not rely on CByteArray having a copy constructor)

        P 1 Reply Last reply
        0
        • A antlers

          You don't need to provide one for CByteArray if you provide one for DATAGRAM (that does not rely on CByteArray having a copy constructor)

          P Offline
          P Offline
          packetlos
          wrote on last edited by
          #4

          Thanks toxcct and antlers, toxcct was right that there in as far as I can see a way to provide a constructor for the struct, therefore I converted it to a class and used CByteArray's .Copy() to provide the assignment. :)

          T 1 Reply Last reply
          0
          • P packetlos

            Thanks toxcct and antlers, toxcct was right that there in as far as I can see a way to provide a constructor for the struct, therefore I converted it to a class and used CByteArray's .Copy() to provide the assignment. :)

            T Offline
            T Offline
            Tim Smith
            wrote on last edited by
            #5

            There is little difference between a class and a structure. The only difference is that the default protection is private for a class and public for a structure. Tim Smith I'm going to patent thought. I have yet to see any prior art.

            T 1 Reply Last reply
            0
            • T Tim Smith

              There is little difference between a class and a structure. The only difference is that the default protection is private for a class and public for a structure. Tim Smith I'm going to patent thought. I have yet to see any prior art.

              T Offline
              T Offline
              toxcct
              wrote on last edited by
              #6

              that's what i was thinking, but in another side, i did never see struct with member functions... so, here was my doubt.


              TOXCCT >>> GEII power

              1 Reply Last reply
              0
              • P packetlos

                Hi there, If I add a CArray such as CByteArray to a struct like so: typedef struct DATAGRAM { // other types here... CByteArray pkt_data; // Packet data } DATAGRAM; and then try and assign something of type DATAGRAM to type DATAGRAM i get the following compile error: C2558: struct 'DATAGRAM' : no copy constructor available or copy constructor is declared 'explicit' I know this is because because CByteArray is of dynamic type so I need to provide a copy constructor. So my question is do I need to provide one for DATAGRAM or do I really need to provide one for CByteArray and if so could somebody post a code snippet to help me out? Cheers Packetlos

                J Offline
                J Offline
                John R Shaw
                wrote on last edited by
                #7

                Remember that a struct in C++ is a class that defaults to public access and a normal class defaults to private access. Since DATAGRAM has a dynamic member it does require a copy constructor:

                struct DATAGRAM
                {
                // other types here...
                CByteArray pkt_data;

                // ctor
                DATAGRAM(DATAGRAM& dg)
                {
                    // copy other type here...
                    pkt\_data.Copy(dg.pkt\_data); // Copy packet data
                }
                

                };

                Note: I did not use a typedef because it is not needed in C++. Good Luck!:) Now if the blasted:mad: power does not go out again, you'll get this message! INTP

                1 Reply Last reply
                0
                • P packetlos

                  Hi there, If I add a CArray such as CByteArray to a struct like so: typedef struct DATAGRAM { // other types here... CByteArray pkt_data; // Packet data } DATAGRAM; and then try and assign something of type DATAGRAM to type DATAGRAM i get the following compile error: C2558: struct 'DATAGRAM' : no copy constructor available or copy constructor is declared 'explicit' I know this is because because CByteArray is of dynamic type so I need to provide a copy constructor. So my question is do I need to provide one for DATAGRAM or do I really need to provide one for CByteArray and if so could somebody post a code snippet to help me out? Cheers Packetlos

                  P Offline
                  P Offline
                  Paul Ranson
                  wrote on last edited by
                  #8

                  I recommend you use a more modern design of container, perhaps std::vector<unsigned char>. These containers provide a copy constructor so that the compiler default copy constructor for your class will work. I don't think you should be having to work out how to copy a CByteArray efficiently, or even correctly. Paul

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups