Jump to content

  • Log In with Google      Sign In   
  • Create Account

What is the purpose of creating a copy constructor?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 Drakkcon   Members   -  Reputation: 612

Like
1Likes
Like

Posted 27 April 2005 - 12:21 PM

I know this is a really newbie question, but please help me out. I've been looking all over the place about this. Supposedly, when creating an object from another object or when passing an object by value as a function argument and using the default copy c-tor, a shallow copy is made which ends up meaning that the two objects point to the same location in memory. When one loses scope the other is deleted (or something like that). This is solved by creating a constructor whose only argument is a reference to an instance of the class. For one, I don't really see why this suddenly solves the problem. The tutorials I looked at just ended there. It seems to me that you would have to use the extra constructor by first instancing the object and then assigning it as an argument to the new copy-constructor, or create a static function that returns this. Can someone explain a less roundabout way of doing this, or why it's really necissary in the first place? The only reason I'm looking at this is because I want to create a singleton and all of the tutorials I could find about singletons used a copy constructor (and I must know how each part of my program is working). Edit: Fixed some spelling errors.

Sponsor:

#2 ascorbic   Members   -  Reputation: 307

Like
0Likes
Like

Posted 27 April 2005 - 12:34 PM

The copy constructor is used on objects/classes that use dynamic memory.
Look below.

class A
{
A();

int C;
char* B;
}

void main()
{
A object1;
A object2;
object1 = object2;
}

if you do this above, the = operator will copy all data from the one object to the other. That's fine for static variables (int, char, etc...). But for data that use pointers(char *, etc...) both objects will point to the same memory location. Now when one object is deleted, the memory location will be deleted. The other object will still "point" to the deleted data, so when you try to access it, you will either get an exception or garbage data.

The copy constructor is defined by you so that each object will get its own memory for the variables that use dynamic memory. So when one object is deleted now, any others don't care and you'll get no problems.

Quick explanation, not the best.

Ascorbic

#3 Drakkcon   Members   -  Reputation: 612

Like
0Likes
Like

Posted 27 April 2005 - 12:35 PM

That's a good explanation. I understand now. For instance if I had two classes both with an SDL_Surface *, and I assigned one to the other, they would both have the same pointer, so when one was destroyed, the pointer points to nothing. Cool. But what's the best way to use that copy constructor when creating the class? Could you use it through the normal constructor?

#4 ascorbic   Members   -  Reputation: 307

Like
0Likes
Like

Posted 27 April 2005 - 12:46 PM

Don't hold me to this, but I believe that if you use a copy constructor, the = operation is overloaded by your specific constructor when you initialize an object. I believe you should overload the = operator also in the same manner.
Anyone else have more insight on this?

class A {...};

A object1;
A object2 = object1; // copy constructot
A object3 = A(object1); // copy constructor

object2 = object1; // = operator since no object being initialized

Ascorbic



#5 Andy Gainey   Members   -  Reputation: 2120

Like
0Likes
Like

Posted 27 April 2005 - 12:56 PM

Quote:
Original post by ascorbic
A object3 = A(object1); // copy constructor

I'm not entirely sure what would happen here. It might actually call the copy constructor twice. A(object1) would cause the copy constructor to be called to create a new temporary object, and then A object3 = ... would cause the copy constructor to be called again, this time object3 is the destination, and the temporary A just created is the source to be copied from. The simplest form for using the copy constructor is:

A object4(object1);

But in reality, the copy constructor is most often called implicitly by the compiler, rather than explicitly by the programmer. You don't typically use it explicitly, but you write it, knowing that it is being used by the compiler when passing objects by value, when returning them from functions, etcetera. A common one that people might sometimes not be aware of is with a lot of STL containers. std::vector::push_back() uses the copy constructor, for example.

#6 Drakkcon   Members   -  Reputation: 612

Like
0Likes
Like

Posted 27 April 2005 - 01:10 PM

Ah, that makes it alot simpler. So the compiler uses it for me? Thanks a lot!

[Edited by - Drakkcon on April 27, 2005 7:10:41 PM]

#7 3DNeophyte   Members   -  Reputation: 122

Like
0Likes
Like

Posted 27 April 2005 - 01:16 PM

As Agony said, the copy constructor is really implicit in nature, and ignoring it can cause you to shoot yourself in the foot, especially if you're passing around linked-lists and other dynamic structures. It's usually good practice to always write a copy constructor, but if you're like me and hate going an extra mile to safeguard against these kinds of situations, always pass around objects via pointers. But then again, using pointers to ignore copy constructors is fine as long as you're throwing around objects within your own code; but if you're using others' libraries like the STL (again, as Agony mentioned), you'll need to write one.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS