INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Use of default constructor over assignment operator

Use of default constructor over assignment operator

Use of default constructor over assignment operator

(OP)
Hi all,

Could some one please tell me what is the advantage of Default constructor over default assingment operator.
Is there something that can be accomplished only by constructor?

Regards,
Srk

RE: Use of default constructor over assignment operator

Initialization of refrences.

RE: Use of default constructor over assignment operator

For example

CODE

int counter;
class Simple
{
public:
   Simple ()
   :  m_counter(counter)
   ,  m_lemon(0)
   {
   }
   Simple (const Simple& that)
   :   m_counter(that.m_counter)
   ,   m_lemon (that.m_lemon)
   {
   }
   Simple& operator =(const Simple& that)
   {
      // m_counter cannot be initialized here
      this->m_lemon = that.m_lemon;
      return *this;
   }
private:
   int& m_counter;
   int  m_lemon;
};
 

RE: Use of default constructor over assignment operator

>what is the advantage of Default constructor over default assingment operator...
What's the advantage of motor plants over automobile sales centres?

You need contructors to create class objects from the scratch.
You need assignment operators to assign existent (created by constructors) objects.

In particular you need default constructor (not assignment operator) to define arrays of class objects (and STL containers too).

An example of a class which can't have correct assignment operator w/o a proper default constructor (othewise you can't declare arrays of this class objects or it's possible to create never-to-assign objects):

CODE

class WithDynArray
{
public:

private:
  double* pData_; // Pointer to dynamic memory chunk
  size_t  dSize_; // This chunk number of elements
};
...
WithDynArray donttreadonme;
...
Let's suppose that WithDynArray has not default constructor. Well, the donttreadonme.pData_ member is not initialized (has garbage value - pointer to nowhere). You can't assign any WithDynArray value to this variable! An assignment operator must deallocate old (target) pData memory chunk but this pointer is undefined in donttreadonme!

Add default constructor:

CODE

...
WithDynArray(): pData_(0), dSize_(0) {}
...
That's OK: now you have a good object donttreadonme (don't forget to change its name now;) and can define correct assignment operator(s) for this class.

Moral: default constructors and assignment operators are not competitors.
 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close