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);
}
returnthis*
}
The Queue class has a front and back pointer to a queue.
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.
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?
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.
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:
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?
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
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?
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.