2 questions on pointers/refs

Started by
3 comments, last by nobodynews 18 years, 8 months ago
1) why do you need to for example have a data member on the heap, when you can assign a pointer to a class ie in a class: private: int * itsAge; all the other functiosn aren't allocated to heap however, but can't you just assign a pointer to class with similar results like Rect * pRect = new Rect(); whats the point/difference to it is probably what im saying, is causing some confusion. 2nd Question, is more confirmation of understanding but when using a function to return a reference ie: int &function(....) this is because you DON'T want to make any copies yea? thanks all, when i undertsnad this i think the hardest part is over...(hopefully) [Edited by - dcuk on September 2, 2005 7:08:29 AM]
Advertisement
Quote:Original post by dcuk
1)

why do you need to for example have a data member on the heap, when you can assign a pointer to a class

ie in a class: private:
int * itsAge;

all the other functiosn aren't allocated to heap however,

but can't you just assign a pointer to class with similar results like
Rect * pRect = new Rect();

whats the point/difference to it is probably what im saying, is causing some confusion.

2nd Question, is more confirmation of understanding but when using a function to return a reference ie: int &function(....)

this is because you DON'T want to make any copies yea?

thanks all, when i undertsnad this i think the hardest part is over...(hopefully)


I don't understand your first question at all but as to your second question there are a few reasons why you would return a reference. The first reason is indeed to avoid a copy of the object but another reason can also be if you want to modify the object. For example, with references you can do things like this:

int& GetSomething () {
return a;
}

and then later:

GetSomething () = 5;

That will assign 5 to the 'a' variable.

Greetings,
Jorrit Tyberghein.Project manager of Crystal Space: An open source 3D engine for Linux, Windows, and MacOS/XURL: http://www.crystalspace3d.org
thanks for the response that helped alot

the first question is basically why would you define a class member function to a pointer when you can do apply teh pointer to the whole class in the first place
come on peeps
I'm not entirely sure what your first question is really asking either. The best I can come up with is you want to know "Why do you put pointers in a class at all when you can just make the class a pointer" ?

If that's not the question then I, too, have no idea what you're talking about. If it is, read on.

The whole point of a clas, in my opinion, is to encapsulate data so you can do stuff without worrying about what's going on inside the object. In this case you want the object to do all the work on the int because you, the programmer, don't want to have to deal with all the newing and deleting all the time, just the one time you make the object. If you made an object and new'd and delete'd it outside the object all the time then you have to take care of all that.

You don't HAVE to do it either way. You don't need classes at all. But it's a design philosophy and in c++ you should try and encapsulate your data with a class and provide an interface to do stuff with the class.

That way you can make a sprite class and then when you want the sprite to do something you just specify an action like "attack" or "evade" and then it will just Work.

Hope that helps

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

This topic is closed to new replies.

Advertisement