copy constructor
-
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 -
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 -
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 -
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)
-
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. :)
-
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.
-
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 PacketlosRemember 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
-
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 PacketlosI 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