Constructor questions

Hiya, just tryin to get a full grasp of constructors. Below are questions that my study guide asks. I'm wondering if I answered these correctly. If not, please guide me! I have no idea what else you should do if a class has pointer member variables beside deleting it if it's initialized... What else would you need to do if you have pointer member variables? If you think you can add on to my answers, it'd be really helpful. Thanks!


1. What is the purpose of a copy constructor?
a. It is just to copy the contents of an object into a a new object.


2. What are 3 things you should do for a class with pointer member variables?
a. Have a destructor that deletes the pointer.
b. ???
c. ???

Last edited on
1. Yes. All constructors do initialize a new object (aka "construct") somehow. From supplied data or otherwise.

2. Seek "C++ the rule of three".
Hmmm. Ok... About copy constructors.... Surely, there is no difference between this...

1
2
3
4
5
6
7
8
myClass A;
myClass B;
B = A;

/*************************/

myClass A;
myClass B(A);


What is the main difference between assigning object B to A and declaring object B with A as its parameter? Is there any difference at all? Anything I look up isn't that understandable...
Ok, so I made this... Can anyone tell me why it is making a shallow copy of itself? I overloaded the assignment, so why?!?!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>

using namespace std;

class myClass
{
    public:
        myClass();
        int *num;
        myClass operator=(const myClass &obj);
        void setNum(int n) {*num = n;}
        void print(){cout << *num << endl;}
};

myClass::myClass()
{
    num = new int;
}

myClass myClass::operator=(const myClass &obj)
{
    num = new int;
    *num = *(obj.num);
    return *this;
}

int main()
{
    myClass info;
    info.setNum(10);
    info.print();

    myClass yes = info; //still shadow copy?
    yes.print();
    yes.setNum(200);
    yes.print();
    info.print();
    return 0;
}

It gives me...

10
10
200
200

What it should be giving me...

10
10
200
10
Last edited on
Never mind. I solved it. It seems that the operator function is only invoked IF the object already exists. In this case, I was declaring and assigning at the same time whereas I should have declared first, and then assigned (2 statements).

1
2
    myClass yes;
    yes = info;
What is the main difference between assigning object B to A and declaring object B with A as its parameter? Is there any difference at all? Anything I look up isn't that understandable...

It depends on the behaviour of the class. The first example creates A and B, using the default constructor for myClass in each case. It then uses the assignment operator for myClass to assign new values to B.

The second example creates A using the default constructor for myClass, and B using the copy constructor for myClass.

What the actual results of doing this will be, depends on how you've written the default constructor, the copy constructor, and the assignment operator.
An important thing to remember is that assignment destroys. The line B = A; destroys the contents of B and fills it with new contents (which happen to match the contents of A). Smartly written assignment could reuse as much as it can, but in general, assignment destroys.

1
2
3
4
5
6
myClass myClass::operator=(const myClass &obj)
{
    num = new int;
    *num = *(obj.num);
    return *this;
}

This is a memory leak, because this operator did not destroy the memory allocated by the default constructor and allocated more. It should either destroy first:
1
2
3
    delete num;
    num = new int;
    *num = *(obj.num);
or reuse:
 
    *num = *(obj.num);

Topic archived. No new replies allowed.