Jump to content
  • Advertisement
Sign in to follow this  
johnnyBravo

returning const pointers, question

This topic is 4816 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've got a class called CMain, which holds the pointer CWindow* window. and the user can create a window by calling createWindow(), which points the window to a new memory, eg new CWindow(). When returning a window, i wanted to return it as a const, so the pointer can't be redirected. const CWindow* getWindow(); but when calling it: ShowWindow(getWindow()->getWnd(), SW_SHOW); i get the error:
Quote:
error C2662: 'CWindow::getWnd' : cannot convert 'this' pointer from 'const CWindow' to 'CWindow &'
So I have to use CWindow* getWindow(); instead, but now the pointer can be modified. Is there any way around this? Thanks btw The place where ShowWindow is being called has inherited the CMain class.

Share this post


Link to post
Share on other sites
Advertisement
On a const object, you can only call const member functions. Make CWindow::getWnd const too:

whatever getWnd() const { whatever }

Share this post


Link to post
Share on other sites
Quote:
When returning a window, i wanted to return it as a const, so the pointer can't be redirected.
const CWindow* getWindow();


Here's what you need to (and have allready) fix. All this does it prevent users from modifying the variables of, or calling non-const functions of, the CWindow pointed to.

Removing the const here will not cause them to be able to modify the pointer "window" within CMain, as getWindow() returns a copy of this pointer.

Plus, you've got the const misplaced if you don't want the pointer itself modified. Mini-tutorial on const and pointers:

CMain main;

CWindow * const window1 = main.getWindow();
window1 = ...; //error: can't modify the pointer, it's const
window1->variable = 3; //ok: can modify the pointee

const CWindow * window2 = main.getWindow();
window2 = ...; //ok: can modify the pointer
window2->variable = 3; //error: can't modify the ponitee, it's const

Edit: Use fruny's example of a function const-qualifier if you want to return a pointer for which the user can't modify variables - but with getWindow() being OK to call, since it dosn't modify any variables :-).

Share this post


Link to post
Share on other sites
Thanks I understand now,

I think i won't const it now, but I wonder if I could hurt anything if I used const on all my functions in every class...
thanks alot guys,

Share this post


Link to post
Share on other sites
Quote:
Original post by johnnyBravo
I think i won't const it now, but I wonder if I could hurt anything if I used const on all my functions in every class...


Your compiler won't even let you on all your functions, if they modify non-mutable variables. As for those that don't, by all means make them const if it dosn't make sense for them to modify variables. getWindow() would make sense to be made const, createWindow() should stay non-const because it modifys the window variable.

Quote:
thanks alot guys,


No problemo :-).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!