Copy Constructor/Assignment Operator/Destructor Combo-requirement

Copy Constructor/Assignment Operator/Destructor Combo-requirement

Post by Edward Estra » Thu, 15 Aug 2002 20:43:57



I'm a little bit confused with the copy-con/assignment oper/destructor
inclusion rule.

I understand the need for adding the destructor for classes that
require copy-con/assignment oper but I'm a bit confused if the reverse
would be needed.

take this for example:
class Abstract {
public:
  virtual ~Abstract() { }
  //...

Quote:};

I added the virtual destructor just so the correct destructor-call of
sub-classes would be called.  Do I need to add empty copy constructor
and destructor for the abstract base class too?

On the same note, what sort of situations would not require me to
include all three?

 
 
 

Copy Constructor/Assignment Operator/Destructor Combo-requirement

Post by Jon Bill » Thu, 15 Aug 2002 21:00:40



Quote:> I'm a little bit confused with the copy-con/assignment oper/destructor
> inclusion rule.

> I understand the need for adding the destructor for classes that
> require copy-con/assignment oper but I'm a bit confused if the reverse
> would be needed.

> take this for example:
> class Abstract {
> public:
>   virtual ~Abstract() { }
>   //...
> };

> I added the virtual destructor just so the correct destructor-call of
> sub-classes would be called.  Do I need to add empty copy constructor
> and destructor for the abstract base class too?

No - the compiler generated defaults would be fine in this scenario. It
sounds like you are thinking of the Rule of Three which says that if you
need one of a copy ctor, assignment op or destructor, then you probably need
all three. This is not a strict rule to be applied in all cases, it is
simply something you should consider when writing classes that require one
of the three. In your particular example, the destructor is not doing
anything other than sitting around being virtual, so the Rule of Three
doesn't apply.

Quote:> On the same note, what sort of situations would not require me to
> include all three?

The one above? <g>

Cheers,

Jon.

 
 
 

Copy Constructor/Assignment Operator/Destructor Combo-requirement

Post by Victor Bazaro » Thu, 15 Aug 2002 21:33:14



> I'm a little bit confused with the copy-con/assignment oper/destructor
> inclusion rule.

> I understand the need for adding the destructor for classes that
> require copy-con/assignment oper but I'm a bit confused if the reverse
> would be needed.

> take this for example:
> class Abstract {
> public:
>   virtual ~Abstract() { }
>   //...
> };

> I added the virtual destructor just so the correct destructor-call of
> sub-classes would be called.  Do I need to add empty copy constructor
> and destructor for the abstract base class too?

> On the same note, what sort of situations would not require me to
> include all three?

No, those are two different issues.  Having the copy c-tor and
the copy assignment op and the destructor is usually required
if your object handles dynamic memory.  Making the destructor
virtual is required if you are going to destroy objects of any
derived class through a pointer to the base.  So, they both do
relate to free store, but from different angles.

Victor
--
Please remove capital A's from my address when replying by mail

 
 
 

1. How to copy vector in copy constructor and assignment operator?

Hi,

I need some help with vectors in a copy constructor and assignment
operator. I am currently using an array, similar to the example below,
but would like to implement something like this with a vector. The array
used below is an array of pointers to derived class objects. So, I would
like to use a vector of pointers to derived class objects. I know how to
insert objects into a vector by using an iterator and push_back() in a
general function. However, I am confused as to how the copy contructor
and assignment operator should do this. Thanks for any help.

const int constNumberOfXTypes = 16;

// Copy constructor.
X::X(X const & rhs)
{
        var1 = rhs.var1;
        var2 = rhs.var2;

        // delete array here...

        for (unsigned int i = 0; i < constNumberOfXTypes; ++i)
        {
                arrayVariable[i] = rhs.arrayVariable[i];
        }

// Assignment operator. Check for assignment to self.
X& X::operator=(X const & rhs)
{
        if (this != &rhs)
        {
            var1 = rhs.var1;
            var2 = rhs.var2;

            //delete [] arrayVariable;
            for (unsigned int i = 0; i < constNumberOfXTypes; ++i)
                arrayVariable[i] = rhs.arrayVariable[i]->Clone();

Class X
{
public:
    X();
    X(X const &);
    ~X();

private:
    vector<BaseType *> arrayVariable;

Regards,

Michael


      [ about comp.lang.c++.moderated. First time posters: do this! ]

2. Sun Ultra bench results

3. Can a POD-struct have a user-declared copy assignment operator or destructor?

4. Using OmniGo to check Email?

5. copy constructor vs. assignment operator

6. MidiShare CD-ROM available

7. Copy constructor vs. assignment operator on uninitialized memory

8. Using a (PC) modem for Internet access on

9. Temporary objects && Copy constructor, assignment operator

10. Derived copy constructors, derived assignment operators

11. overloading assignment operator & copy constructor

12. Copy constructor called in assignment operator

13. what's the difference between assignment operator & copy constructor?