Skip to main content
gamedev.net
🔒 Locked

returning const pointers, question

Started by johnnyBravo 3 replies 400+ views
Original Post
johnnyBravo
johnnyBravo
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.
Fruny
Fruny
On a const object, you can only call const member functions. Make CWindow::getWnd const too:

whatever getWnd() const { whatever }
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
MaulingMonkey
MaulingMonkey
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 :-).
johnnyBravo
johnnyBravo
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,
MaulingMonkey
MaulingMonkey
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 :-).

Topic Locked

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

Sign in to reply to this topic.