3 Replies - 505 Views - Last Post: 21 June 2011 - 04:41 PM Rate Topic: -----

#1 passer_by  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 234
  • Joined: 06-March 11

why is it crucial to define my own CCTOR ?!

Posted 21 June 2011 - 06:45 AM

Hello everybody,

Why is it crucial to define my own CCTOR ? after all , the compiler supplies a default cctor
for every class who doesn't has a cctor , so why do it in any case ?
The only reason that I've come up with , is a case when I have an object , and I make a duplicate out
of it like :
Class someObject {.....};
someObject * obj1 = new someObject ();
someObject * obj2 = obj1;
delete obj2; 

....

// when the code ends , I guess that obj1 would be deleted  , and obj2 would point to garbage 


Thanks,Ron

Is This A Good Question/Topic? 0
  • +

Replies To: why is it crucial to define my own CCTOR ?!

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,435
  • Joined: 25-December 09

Re: why is it crucial to define my own CCTOR ?!

Posted 21 June 2011 - 08:00 AM

Quote

Why is it crucial to define my own CCTOR ? after all , the compiler supplies a default cctor
for every class who doesn't has a cctor , so why do it in any case ?


It is not always necessary to create a constructor. If a shallow copy is acceptable then you do not necessarily need to create a constructor. However it may be advantageous to create a constructor to insure the member variables start with a user defined value.

Quote

Shallow copying

Because C++ does not know much about your class, the default copy constructor and default assignment operators it provides use a copying method known as a shallow copy (also known as a memberwise copy). A shallow copy means that C++ copies each member of the class individually using the assignment operator. When classes are simple (eg. do not contain any dynamically allocated memory), this works very well.


If you have pointer variables then you may need to create a constructor, destructor, and copy constructor to insure the pointers are allocated and de-allocated correctly.

Quote

Deep copying

The answer to this problem is to do a deep copy on any non-null pointers being copied. A deep copy duplicates the object or variable being pointed to so that the destination (the object being assigned to) receives itís own local copy. This way, the destination can do whatever it wants to itís local copy and the object that was copied from will not be affected. Doing deep copies requires that we write our own copy constructors and overloaded assignment operators.


Jim
Was This Post Helpful? 2
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: why is it crucial to define my own CCTOR ?!

Posted 21 June 2011 - 02:12 PM

Classes are not always *simple* and often require some kind of initailization. For example in a GUI library the class Window may need to create and window though the OS's API. Or a BankAccount class may need to initialize required member variables such as customer name and account balance. Imagine if the class somehow didn't get properly initialized and a customer was given an uninitialized account balance!

Think about a class like a Vector class. When you create a vector with the default constructor it should know that the size is zero without someone having to call some "init" function.

And as a comment on Jim's post above: If you are doing deep copying and you find that you need a deconstructor, or a copy constructor, then you will also need an assignment operator. These three functions are strongly related and if you need one of them then chances are very good that you require all three. This is known as the "Rule of Three".

Funny story on that though. I found a place where I needed a copy constructor and an assignment operator but DID NOT need a deconstructor! Like most "Rules" in programming the rule of three is a "thumb rule" or guide and there may be times when it does not apply. In my case I needed a copy constructor and assignment operator to provide a particular interface for using the class (more importantly making the use of class transparent to the programmer who should never need to really know the class existed at all.)
Was This Post Helpful? 1
  • +
  • -

#4 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 687
  • View blog
  • Posts: 2,376
  • Joined: 31-December 10

Re: why is it crucial to define my own CCTOR ?!

Posted 21 June 2011 - 04:41 PM

Just too add one thing, if you make a constructor that takes an argument(s), then the compiler will NOT synthesize a default constructor. So when you go to use that class, you will always need to use the constructor that takes argument(s). If you decide you need an array of objects of your class, you get an error because there is no default constructor. When the compiler creates the array, it default constructs each one, and since there is no default, you get an error.

Another good reason to make your own constructors is that you can have a default constructor that sets everything to let's say zero, and another constructor that creates an object that takes up a lot of memory. If you create an array, the default constructor is used, and not that much memory is used until you start creating objects with the other constructor.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1