Help with a copy constructor.

I'm given this code and I have to write a copy constructor for it, however I don't understand how pointers work really and I'm just getting confused. Any help would be greatly appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Grade{
private:
   int a, b, c;
   int *d, *e;


public:
   Grade(const Grade& orig){





   }
}
If you do not understand how the pointers work in this class ( I also do not know ) when define the copy constructor simply

Grade(const Grade& ) = default;
Last edited on
but what would I write to finish the copy constructor then??
I'm new to constructors as well, but I believe you have to create a default constructor as well

1
2
public:
Grade();


Plus, they are supposed to copy something...

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
//===== grade.h ============================================

class Grade
{
private:
   int a, b, c;
   int *d, *e;

    public:
        
        Grade();

        Grade(const Grade& p);   // copy constructor
        
//=== grade.cpp ============================================

Grade::Grade(const Grade& p)  //setting the value (::) of the constructor Grade to your copy constructor
{
    a = p.a;
    b = p.b;
}
  
//============ main.cpp ====================================

Grade p;            // calls default constructor.
Grade x = p.a;  // calls your copy constructor.


Granted, don't quote me on this, like I said, I'm still new at this, but this is the basics behind how I understand it.
Last edited on
A copy constructor is created by default that will assign all member variables to be the same as they were in the original object.

For a simple object that has no pointers this is usually sufficient. However, when copying a class that contains a pointer you need to write your own implementation of the copy constructor to handle the allocation of new memory for the new objects pointer.

http://www.cplusplus.com/articles/y8hv0pDG/ - Copy Constructors
http://www.cplusplus.com/doc/tutorial/dynamic/ - Dynamic Memory
closed account (zb0S216C)
Because no-one else mentioned it, if a class contains either a reference or constant member, the compiler will not be able to generate the copy constructor/assignment operator.

Wazzak
I still dont understand what code I need to produce after the Grade(const Grade& orig){

Like, what code is missing in the copy constructor is there anymore code that goes there or is that all I need?
closed account (zb0S216C)
You're missing the initialiser-list:

1
2
3
4
5
Grade::Grade( Grade const &Grade_ )
  : a( Grade_.a ), b( Grade_.b ), c( Grade_.c ),
    d_( Grade_.d ), e( Grade_.e )
{
}

I don't know what "Grade::d" and "Grade::e" point to, so copying them might not be a good idea.

Wazzak
Last edited on
Framework, that's what I'm confused on, he gave us this code as a homework assignment and we have to "Create a copy constructor for the following class that creates a duplicate of the object." But i have no clue what d and e point to... so idk lol.

And would your source code go after Grade(const Grade& orig){??
closed account (zb0S216C)
Look at my code. The initialiser-list sits between the parameter list and the constructor's body.

Wazzak
In your copy ctor, an instance of Grade object 'p' is passed in by a const reference. A reference is not a pointer. Const simply enforces that the passed in object will be "read-only".

What you need to do inside the copy constructor (ctor) is perform a member by member copy from object p.

You will need to perform a deep copy of the data pointed to by pointers d and e

For exampl, inside your ctor

1
2
3
4
5
6
this->e = 0;   // set member pointer e initially to null
if( p.e )
{
   this->e = new int;
   *(this->e) = *(p.e);
}


Note: this->e will have no previous value since you are creating a new object and copying value from a referenced object. You also must dereference a pointer with '*' because you want to change the value the pointer contains, without it you would be changing the pointer itself which is simply a memory address.

this->e could also be a pointer to an integer array, however there is no size member that tells you how big this array could be, so for the example it allocated a int on the help using the new operator.

in your class destructor( dtor ) make sure to free the memory

1
2
3
~Grade() {
   delete this->e;
} 


Note: you don't need to check for null before deleting this->e

You should have enough info to code-complete a deep-copy ctor.

Framework example also works, but it only performs a shallow-copy, after the copied object has been created, two objects will point to the same thing for member pointer d and e, this may not be what you want because if one object modifies these value the other object will also be mutated likewise. Thus a deep-copy gives each object it's own copy of all values.

---
Rajinder Yadav <labs@devmentor.org>

DevMentor Labs
http://labs.devmentor.org
Creating Amazing Possibilities
Last edited on
closed account (zb0S216C)
DevMentor wrote:
"A reference is not a pointer."

That depends on the compiler. The standard does not specify how the reference should be implemented. Therefore, the compiler is free to implement the reference in any way they feel so long as it [the reference] behaves in accordance with the standard. Typically, references may be implemented with a pointer, or, each reference may be replaced with the address of the referent. Read your compiler's documentation.

DevMentor wrote:
"Const simply enforces that the passed in object will be "read-only"."

There's more to it than that. A reference to a constant type allows temporary instances to be bound to the reference which extends the life-time of the temporary to that of the reference that it's bound to -- the reference has to be constant because temporary storage is R-value storage.

DevMentor wrote:
"What you need to do inside the copy constructor (ctor) is perform a member by member copy from object p."

The compiler is perfectly capable of performing member-wise copying on each of the data-members so long as the data-members are trivially copyable. Data-members that are either references or constant cannot be trivially copied. But because pointers are involved, member-wise copying is sufficient unless the pointers are not allowed to point to the same resource, in which case a deep-copy is required.

DevMentor wrote:
"Framework example also works, but it only performs a shallow-copy, after the copied object has been created, two objects will point to the same thing for member pointer d and e, this may not be what you want because if one object modifies these value the other object will also be mutated likewise."

I know I was performing a shallow-copy. I did say:

Framework wrote:
"I don't know what "Grade::d" and "Grade::e" point to, so copying them might not be a good idea."


Wazzak
Last edited on
@Zaita
A copy constructor is created by default that will assign all member variables to be the same as they were in the original object.

For a simple object that has no pointers this is usually sufficient. However, when copying a class that contains a pointer you need to write your own implementation of the copy constructor to handle the allocation of new memory for the new objects pointer.



It is an incorrect statement. A class can contain pointers as its members but it does not allocate memory for them In this case it is not responsible for deleting the memory. As in the initial post it is not clear who allocates the memory then the copy constructor can be defined as default. So it is only your fantasy about allocating a memory by the class. There is no enough information in the original post that to make such statements.
Topic archived. No new replies allowed.