const help
-
Hello. This is a general C++ question. Can somebody please shed some light on the const modifier? I have been programming for about a year.. and I never use const because I really don't understand it... so I am trying to gain some ground, and I just can't understand this. Please look at the class declaration below, and can somebody explain to me why the functions draw(), and getx() allow me to modify the private data members. they are declared const, so they shouldn't allow me to modify those variables.. but they do. according to MSDN help library, you declare a member function const by placing the keyword after the parameter list.. this is what I did.. but it still lets me modify them. I know if I declare the variables const it will work.. but shouldn't I be able to declare a function const so the compiler won't let me modify? class cRectangle { public: // constructors cRectangle(); cRectangle(int nL, int nW); // Destructor ~cRectangle(); // public accesors void SetLength(int nL); void SetWidth(int nW); int GetLength() const { return *nLength;} int GetWidth() const { return *nWidth;} // public methods void Draw() const; private: // Data Members int* nLength; // pointers because they will be stored on the heap int* nWidth; }; // implementation file // striped but draw() function // Drawing functions void cRectangle::Draw() const { // try and change a variable *nWidth = 42; // why its this allowed???? // function is const? should not let me change // the object. } If anybody could explain this to me. It would be greatly appreciated. Thank you.
const prevents the method from changing the value of any data members. In this case, it's the pointers. You're changing the value of what is being pointed to.
he he he. I like it in the kitchen! - Marc Clifton (on taking the heat when being flamed) NEW: Awasu v0.7[^]: A free RSS reader with support for Code Project.
-
because nWidth is a pointer to an int, not an int. your Draw code is changing the value that nWidth points to, not nWidth itself. and it will probably give you an access violation, since nWidth isn't initialized. if you change the Draw code to "nWidth = 42", it will not compile. -c
A | B - it's not a choice.
Damn - looks like you can type faster than me. :-) Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
C# will attract all comers, where VB is for IT Journalists and managers - Michael P Butler 05-12-2002
It'd probably be fairly easy to make a bot that'd post random stupid VB questions, and nobody would probably ever notice - benjymous - 21-Jan-2003 -
Damn - looks like you can type faster than me. :-) Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
C# will attract all comers, where VB is for IT Journalists and managers - Michael P Butler 05-12-2002
It'd probably be fairly easy to make a bot that'd post random stupid VB questions, and nobody would probably ever notice - benjymous - 21-Jan-2003i even made a quick project to test it out, just to be sure! :) -c
A | B - it's not a choice.
-
i even made a quick project to test it out, just to be sure! :) -c
A | B - it's not a choice.
You're a machine, I stand in awe.... All I did was type. Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
C# will attract all comers, where VB is for IT Journalists and managers - Michael P Butler 05-12-2002
It'd probably be fairly easy to make a bot that'd post random stupid VB questions, and nobody would probably ever notice - benjymous - 21-Jan-2003 -
You're not changing nWidth, you're changing the value stored in it. Why is nWidth a pointer ? When your function returns, it will still point to the same place. Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
C# will attract all comers, where VB is for IT Journalists and managers - Michael P Butler 05-12-2002
It'd probably be fairly easy to make a bot that'd post random stupid VB questions, and nobody would probably ever notice - benjymous - 21-Jan-2003ok. I understand kind of.. even if a function is constant, it can still modify what a pointer points to.
-
ok. I understand kind of.. even if a function is constant, it can still modify what a pointer points to.
MAybe this will help - if you create a pointer you can do this: const int * p_i, or this: int * const p_i the second syntax makes the value const, the first the memory address. A const function only enforces the first idea. const int * const p_i is also valid syntax for both. Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
C# will attract all comers, where VB is for IT Journalists and managers - Michael P Butler 05-12-2002
It'd probably be fairly easy to make a bot that'd post random stupid VB questions, and nobody would probably ever notice - benjymous - 21-Jan-2003 -
MAybe this will help - if you create a pointer you can do this: const int * p_i, or this: int * const p_i the second syntax makes the value const, the first the memory address. A const function only enforces the first idea. const int * const p_i is also valid syntax for both. Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
C# will attract all comers, where VB is for IT Journalists and managers - Michael P Butler 05-12-2002
It'd probably be fairly easy to make a bot that'd post random stupid VB questions, and nobody would probably ever notice - benjymous - 21-Jan-2003Thank you. I understand what is going on. I don't want to keep asking stupid questions.. but if that is the case, then how would you solve this problem. suppose you have a class, and this class has some pointers to different objects.. now some functions of the class need to be able to modify the data of what the pointer points too, but you also have some functions that just need to retrieve the data, and you want to make sure that those functions do not modify the pointed to data.
-
Thank you. I understand what is going on. I don't want to keep asking stupid questions.. but if that is the case, then how would you solve this problem. suppose you have a class, and this class has some pointers to different objects.. now some functions of the class need to be able to modify the data of what the pointer points too, but you also have some functions that just need to retrieve the data, and you want to make sure that those functions do not modify the pointed to data.
How about int * const pTemp = m_pInt; so in the function you always access a const pointer ? I can't think of an iron clad solution, i.e. one that does not rely on the functions implimenter 'playing by the rules'. Unless I have external clients, I don't use const functions much at all, and if you declare it const and don't modify anything, then the fact that you could is immaterial, you made a contract with your classes clients and you kept it. Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
C# will attract all comers, where VB is for IT Journalists and managers - Michael P Butler 05-12-2002
It'd probably be fairly easy to make a bot that'd post random stupid VB questions, and nobody would probably ever notice - benjymous - 21-Jan-2003 -
How about int * const pTemp = m_pInt; so in the function you always access a const pointer ? I can't think of an iron clad solution, i.e. one that does not rely on the functions implimenter 'playing by the rules'. Unless I have external clients, I don't use const functions much at all, and if you declare it const and don't modify anything, then the fact that you could is immaterial, you made a contract with your classes clients and you kept it. Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
C# will attract all comers, where VB is for IT Journalists and managers - Michael P Butler 05-12-2002
It'd probably be fairly easy to make a bot that'd post random stupid VB questions, and nobody would probably ever notice - benjymous - 21-Jan-2003Thank you Christian. That was the answer I came up with, but with my limited experince, I just wanted to make sure.
-
const prevents the method from changing the value of any data members. In this case, it's the pointers. You're changing the value of what is being pointed to.
he he he. I like it in the kitchen! - Marc Clifton (on taking the heat when being flamed) NEW: Awasu v0.7[^]: A free RSS reader with support for Code Project.
Taka Muraoka wrote: const prevents the method from changing the value of any data members Not quite. const is a contract, a promise, to not change the logical state of an object, when applied to a member function. Imagine a class that has a backing store (e.g. a database) for the data it can present to the user. Any GetFoo() should probably be a const member function. Later on you realize "Hey, this stuff is waaay to slow. I need a cache!". You implement such a cache. But, that would change the objects state, right? No. This is where the
mutable
keyword come in play. A const member function may modify mutable qualified data - even that the "contract" you have with the clients of that code promises that it may not change the objects logical/observable state. This is just scraping the top of the iceberg. Const correctness is an artform in itself, and depending on scope it can mean a whole lot of things. For further information, I think Marshall Cline's C++ FAQ Lite could be a good starting point.