Skip to main content
gamedev.net
🔒 Locked

copy operator

Started by tendifo Jan 15, 2007 at 3:13 PM 2 replies 450+ views
Original Post
tendifo
tendifo
In the following code:
class A { ... void do_something_else(); ... };

void do_something(A var)
{
   var.do_something_else();
}

int main()
{
   A a_variable;
   do_something(a_variable);
}
How is a_variable copied to var? If let's say A contains two 4-byte ints.. does the program just copy 8 bytes of data straight from a_variable to var? Is there a way to control what gets copied and how? I tried the = operator but that didn't work, or at least it didn't seem to work. Thanks.
Vorpy
Vorpy
It is created with the copy constructor. The copy constructor is used to construct a new copy of an object from an already existing instance. The assignment operator, operator=, is only used on already initialized objects.

A copy constructor is a constructor whose argument is an instance of the object passed by reference (not by copy because a copy constructor is needed to make a copy).

If the programmer does not provide a copy constructor then a default copy constructor is created that initializes all data members by using their copy constructors.
Zahlman
Zahlman
There is no "copy operator". There are copy constructors, and assignment operators - one each per class.

When a parameter of object type is passed by value, it gets copied using its class' copy constructor. (In general, the copy constructor is used whenever the compiler sees a need to copy an object. You don't necessarily have control over how many copies occur, however. The compiler is allowed to assume that the copy constructor "does the right thing", so make sure it really does.)

A class wherein no copy constructor is defined gets a default copy constructor instead: this copies the structure member-wise (i.e. members of class type, as well as base classes, get *their* copy constructor invoked; primitive members get copied bitwise; the vtable, if your implementation uses them, doesn't need to be "copied" per se because it's always the same for a given class). This is fine when all members of your class are concrete data types (i.e., either properly designed classes that have in turn provided a correct copy constructor, or non-pointer primitives). Otherwise, you almost certainly need to write a custom copy constructor - as well as an assignment operator and destructor. You will probably also need these if you have any 'const' data members, or members held by reference. (Don't remember the exact details, and IIRC the rules are a bit more restrictive than logic would dictate.)

The copy constructor is written as a constructor which accepts one argument: an instance of the same class, passed by reference (normally a const reference). In particular, constructors accepting base or derived classes, or a single template argument, will *not* be recognized as a copy constructor, even with the argument passed by const reference. (Obviously you can't pass it by value, because the instance to be copied would have to be copied first to be passed to the constructor - infinite recursion.) If you have such a templated constructor and you also need a copy constructor, just overload the template with the proper copy constructor.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.