Copy constructior vs Overloaded assignment

Whats the difference between a copy constructor or an overloaded assignment?

Is it that copy constructor implies that an object is created whereas with overloaded assignment an object may already be in existence?
Exactly.
1
2
3
4
5
6
7
8
9
struct Mine{
   int workers;
};

int main(){
   Mine thisone, thatotherone;
   Mine thatone(thisone); //Calls copy constructor
   thatotherone = thatone; //Calls assignment operator
}
Also note
 
Mine thisOne = something; //calls copy constructor. 

1
2
Mine thisOne;
thisOne = something; //calls assignment operator. 
Last edited on
Also a overloaded assignment must return a reference object so that it can perform other assignments if necessary.
Last edited on
Also one other question. Can you call the destructor of a class within a member function. I.e can I call the destructor within the overloaded assignment function?

So would the following be allowed:


1
2
3
4
5
6
7
8
9
Queue&::operator=(const &Queue ox)
{
if (this.front != ox.front)
{
        this.~Queue();
        this = this.Queue(ox);
}
return this*
}


The Queue class has a front and back pointer to a queue.
Last edited on
Yes. The destructor of an object can be called explicitly, but it is possible for it to lead to undefined behavior (i.e., you call the destructor, then the destructor is called again once the object leaves scope).
Refer to the second post on this thread, it describes it pretty well:
http://stackoverflow.com/questions/3291507/does-explicitly-calling-destructor-result-in-undefined-behavior-here

EDIT:
Regarding your edit, I would not recommend doing that. Destructors are reserved for deleting objects entirely. If you're implementing a queue class, consider creating a function (dealloc, for example), to clear the internal data of the container, instead of having the destructor do it. Then the destructor could also call dealloc upon deleting the object.
Last edited on
OK brilliant I'll look into the dealloc function. Do you know why I have to return this* (a pointer) rather than the actual object here? Could i not just return this as its passed as reference?
this is a pointer. You have to dereference it to return a reference to the object that this points to.
hmmm. OK. how come it works for the following then:

1
2
3
4
5
6
7
8
9
10
11
12
vector<int>& operator+=(vector <int>& v1, vector <int> v2)

{
	while(v1.size() > v2.size())
		v2.push_back(0);
	while(v2.size() > v1.size())
		v1.push_back(0);
for(int n = 0; n < v1.size(); n++)
	v1.at(n) += v2.at(n);

return v1;
}


Last edited on
How is that related to your previous question?

EDIT:
Oh. I think I see now. When you define vector<int>& operator+= it is not a member function of the vector class. Therefore, the this pointer doesn't exist. So you return the left hand side argument, in this case, v1.
In the prior example, Queue& Queue::operator=(Queue const &rhs), operator= is a member function, where the lhs argument is implied to be this. So you return *this to return the left hand side argument.
Last edited on
Oh i see. Its because this is a pointer so you can't just return this because then you are returning a pointer. It is expecting a reference parameter. So by returning this* you are returning what this is pointing at, essentially an object.

And in the other instance you cant return v1* because v1 is not a pointer.

My confusion was based around me jumping to the assumption that by passing a reference parameter you are passing a pointer. Essentially an address that is actually the object. But it seems that this premise is false...


What is the difference between a reference parameter and a pointer?

This leads me to my confusion:

1
2
3
4
5
int x = 3; 

int* p; // pointer of int

p = &x; // assign the pointer the address of x 


Atm this makes more sense to me but it doesn't compile:

1
2
3
4
5
int x = 3;

int* p;

p* = &x;


What am I not understanding?
Last edited on
1
2
3
4
5
6
int x = 3;  // x is an int.

int* p; // p is the address of (or a pointer to) an int.

p = &x; // set p equal to the address of x
        // (or, iow, make p point to x)  


1
2
3
4
5
6
7
int x = 3; // x is an it.

int* p; // p is an (uninitialized) address of an int
        // (iow, p points to someplace random in memory)

*p = &x; // treat the int at the (random) address p contains as if
         // we own it (we don't) and store the address of x in it. 


You can't assign an address to an int without casting of course, so the compiler disallows the second version.

Note that p* is a syntax error.

What is the difference between a reference parameter and a pointer?

A reference is an alias for a variable. A pointer is an address.
Cool. So a reference is an alias for a variable. A pointer is an address.

I imagine that given the alias for the variable you can find the address, so essentially it is like a pointer but with more information?

You can't assign an address to an int without casting of course, so the compiler disallows the second version.


*p = &x;

In this *p is essentially an int and &x is an int alias for a variable, a reference. This is why this does not work.

Is there anyway of performing the "logic" of above? i.,e assigning the location of the pointer (an int assigned memory location) with the address of x?

Basically the only thing that is programmer facing that deals with actual addresses are pointers?
Last edited on
and &x is an int alias for a variable, a reference.

Back up a little more: the character & in that example has nothing to do with references. Like most other characters (*, =, etc), it means different things in different contexts:
1
2
3
int n = 1 & 2; // the bitwise and operator: produces an int (for completeness)
int*p = &n; // the address-of operator: produces a pointer to int
int& r = n; // reference specifier: combines with the int to the left to declare the type of r to be reference-to-int 

Last edited on
*p = &x;

In this *p is essentially an int and &x is an int alias for a variable, a reference. This is why this does not work.

No. In this context, & means "the address of". So &x is the address of x, which is the same as saying it's a pointer to x.
So when the & sign is used with an identifier it means the address of that identifier

However when it is used with a type it means its create a reference of that type. Essentially an alias for that type.


Is this still true for passing parameters as reference? Is an identifier the same as an alias and thus when you pass an alias of the same type that is equal to the identifier you are essentially passing the alias to the actual object and address associated with it?


Last edited on
Is this still true for passing parameters as reference? Is an identifier the same as an alias and thus when you pass an alias of the same type that is equal to the identifier you are essentially passing the alias to the actual object and address associated with it?

Yes. Passing a reference into a function/method allows you to change the value of it, and have that value change also apply to the object in the calling code.

It's an alternative to passing memory addresses (i.e. pointers) into a function to allow you to change the state of the parameter in the calling code. There are some subtle nuances to using references instead of pointers, which any decent tutorial source should explain for you.
Topic archived. No new replies allowed.