Sign in to follow this  

With get method, use pointer for classes in c++?

This topic is 4824 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

Hi, I was wondering when you have a get method in a class which returns a declared class, should i return a pointer to save memory? eg
class App {
private:
   CWindow cwnd;
public
   CWindow* getCWindow(){return &cwnd;}
   //instead of
   //CWindow getCWindow(){return cwnd;}
};

thanks

Share this post


Link to post
Share on other sites
Depends on what you intend to do with it, and how big it is, generally speaking.

Using a pointer results in having a pointer to the actual memory of the original variable. The bonus is that the fact that there is no copying done results in better speed, especially when you're talking large chunks of data, etc. The downside is that if you do anything to it, you change the actual memory, and if you're using threading there are caveats regarding the fact that it might not be constant or even persistent.

Using a plain non-pointer type variable results in a copy being made. This can be problematic for large structures in the time and memory department, but for small things it's fine. Also, you don't have to worry about changing something you didn't intend to, or having the memory yanked out from under you in another thread.

So, basically it's usage. For example, if I have an entire video frame, I'm going to want to use a pointer to move it to display so I don't have to allocate multiple versions of a huge chunk of memory, but the pixel values of the target I would most likely just pass and allow to be copied.

In the case of windows in an app you probably want a pointer, because generally you're getting the window in order to do something like send a message to it, and sending a message to a copy of the window wouldn't give the results you want on the version of the window you can actually see.

-fel

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by johnnyBravo
Hi, I was wondering when you have a get method in a class which returns a declared class, should i return a pointer to save memory?


Considere using references


class App {
private:
CWindow cwnd;
public
CWindow& getCWindow() { return cwnd; }
CWindow const& getCWindow() const() { return cwnd; }
};

Share this post


Link to post
Share on other sites
I'd recommend you to not return a non-const reference, because if you consider to return by value, you obviously don't want to be able to modify to contens.

Share this post


Link to post
Share on other sites
Returning a non-const referance would defeat the purpose in having the get function to start with. Code external to the class can directly modify the variable and as such you might as well have simply made it public. Personally even the const referance is questionable. It really defeats the reason to use a get function as well. Returning a referance implies there is something to referance. The primary reason to use a get function is to allow you to remove the variable in the future. You thought it would be cool to store 101 state variables for the graphics API, but later come to the conclusion it is a wasted duplication of effort since the graphics API is already doing it.

Share this post


Link to post
Share on other sites
make get/set inline and you can forget about copy overhead.
edit: i mean, of course, if it's not virtual. But do you really need virtual get/set ?

Share this post


Link to post
Share on other sites

This topic is 4824 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