# Having a const class member equal to arguement in classes constructor?

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

## Recommended Posts

Just wonder if this is possible. See, my window class has a vector of buttons... and each button has an unsigned char "parent", which is equal to the window classes unsigned char "ID" that owns said button. The problem is, *somehow* one of my buttons is changing its parent ID, which of course is not good. It's resulting in not being able to find its parent as it doesn't exist, and then you have an error. So I'd like to make that unsigned char a constant, but of course I can't do that in the constructor since it's well... a constant. It finds out the value of "parent" as it's passed in one of the constructor's arguements. So is there a way to go about making this happen, or am I going to have to make a work around? Thanks in advance! -Etyrn

##### Share on other sites
You can initialize your const members in the initializer list... example:

class MyClass{private:	const unsigned int m_Value;public:	MyClass(unsigned int value) : m_Value(value)	{	}};

But seriously, you should investigate why the ID changes... it seems like you're trying to avoid a bug, instead of fixing it.

You can try to set a memory breakpoint on your ID, so as soon as it will be changed, the debugger will break and let you know what happened.

Hope this helps!

##### Share on other sites
My IDE's debugger (GDB) gives me the BSOD everytime I try to use it. I'd love more than anything to get that working, but I guess 'til then I gotta make do.

Anywho, thanks for the constant help. :)

-Etyrn

Edit: How about that. Its parent ID is still managing to change to "4" even as a constant! I guess the button pointer is getting corrupted or something. I guess I'll have to get that thar debugger working. O_o

##### Share on other sites
const is only checked at compile time, and even then it is fairly easy to circumvent (using casts, aliasing, etc.). const is a hint to you that you shouldn't be changing things, not a bulletproof guarantee that the thing can never be changing.

Your bug does indeed sound like bad pointer voodoo of some kind. I highly recommend working on getting your debugger fixed; debugging is an integral part of programming, and you're seriously hamstrung without good debugging tools.

What's the BSOD error you receive? If it's that reliable (running GDB always trips it) then chances are it's probably driver or configuration related and therefore relatively simple to fix.

##### Share on other sites
Quote:
 Original post by EtyrnMy IDE's debugger (GDB) gives me the BSOD everytime I try to use it.

An actual Blue Screen Of Death? [tears] Or just a General Protection Fault?

Quote:
 Its parent ID is still managing to change to "4" even as a constant!

There are two types of constants. Constants that the compiler can figure out at compile-time and thus directly replace in the program. e.g. const int x = 5;. Those cannot be modified, because they don't really exist as variables at run-time. Other constants, such as your const member variables, really are variables which the compiler prevents you from modifying via the regular channels by flagging as invalid code that violates the const-correctness rules. But it is very simple to fool the compiler into letting you modify the "constant" anyway, generally through the use of pointers and/or casts, including const_cast, which exists for that single purpose. [smile]