Skip to main content
gamedev.net
🔒 Locked

References and Pointers

Started by Third Aug 4, 2003 at 9:03 PM 6 replies 400+ views
Original Post
Third
Third
I was reading the advanced functions chapter in Sams Teach Yourself C++ in 21 days today, and I came across the section on returning the this pointer, this is the source code for it:
//a program to demenstrate overloading the prefix operator


#include <iostream>

using namespace std;

class Counter
{
public:
    
    Counter();
    ~Counter(){}

    int GetItsVal() const
	{
		return itsVal;
	}

	void Increment()
	{
		itsVal++;
	}

	const Counter& operator++ ();
	

private:
	int itsVal;
};

Counter::Counter(): itsVal(0)
{}

const Counter& Counter:: operator++ ()
{
     itsVal++;
     return *this;
}


int main()
{
Counter i;
       cout << "The Value of i is " << i.GetItsVal() << endl;
	i.Increment();
	cout << "The value of i is " << i.GetItsVal() << endl;
	++i;
	cout << "The value of i is " << i.GetItsVal() << endl;
	Counter a = ++i;
	cout << "The value of a: " << a.GetItsVal() << endl;
	cout << "and i: " << i.GetItsVal() << endl;
	return 0;
}
And I was wondering why in the operator prototype it shows that it returns a reference, but it really returns a pointer?
hehenoobhehe
hehenoobhehe
quote:
Original post by Anonymous Poster
Actually, it dereferences the this pointer, which is what *this means. And since "this" is a pointer, "*this" is the object it points to. It is expensive to return an object because you have to copy all of the data in the object. It is much faster to just return a reference to the object.


you say this:
-"this" is a pointer,
-"*this" is the object it points to

==>when we return *this, we return the object, dont we?

Then how is this any different from returning an object?

Perhaps, you could elucidate this with difference if the function were to return an object VS returning a reference to the object.

Thanks in advance!

PS. Why do you post as anonymous poster?


krez
krez
a reference is just like a pointer, but it cannot be NULL, and syntatically it is treated as an object rather than a pointer (i.e. use the "." instead of "->" for member functions and such).

so, returning a reference will actually return the address of the object, just like a pointer. but, the function that is getting this value can treat it as an object.
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
Third
Third
Ok, so when I dereference a pointer, it basically becomes a reference?

[edited by - Third on August 5, 2003 2:34:49 AM]
Sneftel
Sneftel
Not quite. When you dereference a pointer, you get an lvalue, not a reference. lvalues act a lot like references, and can be converted to and from references. However, if you do something like this:
double *d;
size_t s = sizeof(*d);
you''ll find that it sets s based on the size of a double, not the size of a reference.


How appropriate. You fight like a cow.
Steadtler
Steadtler
quote:
Original post by Third
Ok, so when I dereference a pointer, it basically becomes a reference?

[edited by - Third on August 5, 2003 2:34:49 AM]


No, it becomes a lvalue, but the "&" in the return type of the function signature make it returned BY reference.

Return parameters are not much different from other parameters. Say you have this function:

----
void myFunction(double &Parameter);

double Toto = 5.0;

myFunction(Toto);
----

Toto is not a reference nor a pointer, but its passed to the function by reference, because of the "&".

Hope that helps!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.