Sign in to follow this  

2 questions on pointers/refs

This topic is 4519 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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,

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

This topic is 4519 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this