Recommended Posts

caesar4    100
for my linked list implementation, i have these functions that take a const T &obj, now, the question is: after i allocate the new link node, which is better to use: memcopy(&link->data,&obj,sizeof(T)) ? or link->data = obj?

Share on other sites
you should use the copy constructor or the objects assignment operator since that's the only way that will work for most user types.

memcpy can only be used with POD (plain old data) types (think C struct's).

Share on other sites
caesar4    100
copy constructor?
like:
class T{
public
T(const T &cp){}
}?
or something else?

Share on other sites
yup that's a copy ctor alright :)

simply put what you want is op= or if you can make it a pass-through to the node construction you can get away with not first creating a default object just to later overwrite it.

Share on other sites
snk_kid    1312
It seems like your going in the wright direction but don't know enough to do it wright [smile]. Yes if you want to implement containers with value semantics then to be efficent it should seperate de/allocation and construction/destruction. How you do this in C++ goes something like this:

void* buf = ::operator new(sizeof(foo)); //allocate memory only....foo* f = new(buf) foo(); //construct (initialize) only , it's called placement new operator and it's in the header *new*....f->~foo(); // destruct only, if you use placement new operator you must explicitly invoke            // destructor of user-defined types,            // if this is template code just do it and the compiler sorts it out if it's not a user-defined type....::operator delete(buf); // deallocate memory only

Note that you can also use any constructor for placement new operator there-for you can initialize the allocated memory with some example value via copy constructor i.e:

foo* f = new(buf) foo(foo_value);

That should give you a hint on how to do what your trying to do.

On a side note if your not doing this to learn data structures & algorithms really you should be using the standard library list.

Share on other sites
caesar4    100
what about objects without copy constructors?

Share on other sites
Quote:
 Original post by caesar4what about objects without copy constructors?

That can actually only happen in the rare instance that the class author explicitly denies it. If a cctor isn't declared the compiler will generate one that's similar to a bitwise copy.

Share on other sites
snk_kid    1312
Quote:
 Original post by caesar4what about objects without copy constructors?

Not sure if you saw my post but anyways, all types are copy constructable (including primitive built-in types) there is how-ever one exception to that rule i'll get to that shortly. If a type does not have a user-declared/defined copy constructor or assignement operator one is implicitly defined by the compiler that by default does member-wise copy.

The exception to this the rule that i mentioned earlier is for user-defined type that declare the copy constructor and/or assignment operator private/protected that means the type does not have value semantics. If clients still want to store these types they must store pointers to that type insteads. This is how the standard library containers work aswell.

Share on other sites
snk_kid    1312
Quote:
 Original post by DigitalDelusionIf a cctor isn't declared the compiler will generate one that's similar to a bitwise copy.

Well that depends on the context, if it's not a struct/class-POD type the default will be a member-wise copy not bitwise.

Share on other sites
Quote:
Original post by snk_kid
Quote:
 Original post by DigitalDelusionIf a cctor isn't declared the compiler will generate one that's similar to a bitwise copy.

Well that depends on the context, if it's not a struct/class-POD type the default will be a member-wise copy not bitwise.

Correct.