More about const

Hello again C++ Experts.

Well... Every thing is going ok with my beginning in C++.
But 'Const' is being a chalenge to me :-)

Y now this... For instances:
const Wm_Click = 14589... It means that every time I put Wm_click in my code,
it is going to be translated into 14589. (Well... I know I invented Wm_Click
and it's number, it is just an example)

But I don't understand it

What is a const member?.

For instances into a class I saw something like this.

const int C; // A class member I guess.
As I know, if I do something like this:
C = 125; // I get an error... Am I right?.

Could anyone explain me the const member?
How they work, etc...

I tried to do something like this

int const m_meber; //(in base class)

Obviusly it gave me an error:
m_member must be initialized in constructor base/member initializer list.
I don't know what it means... I saw something like this in an example...

I also saw this:

c_class (const c_class& Var)

I know that it is a copy constructor... But why the parameter is declared as

Is it right to say that const warranty that the original class object will not change?... What happen if I don't say 'const'

Thanks to all experts.

P.D: I enjoying learning C++, Well... It is a litle more hard than VB, But
I think it is worth of learning...

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Basically const means that the compiler wont allow you to change a variable, once its initialized. And you get once chance to initialize it. If its a 'common' variable, it must be initialized when it is declared. Const members must be initialized in the constructor

class A
   const int ci; // = 5; // initialize here or in constructor
   int anInt;
   A(): ci(5), anInt(0)  {}
   A(int b) : ci(b) {}
   A(const A& a) : ci( {}

   void method() { anInt = anInt + ci; }
   void const_method() const;
   void const_method_2() const;


>>   A(): ci(5), anInt(0)  {}
>>   A(int b) : ci(b) {}
The stuff behind the colon is called the base/member initializer list. This is your last chance of initializing the const member

>>   A(const A& a) : ci( {/* code */ }
The copy constructor. The const means that this function will not modify the passed object a

>>   void const_method() const;
The function const_method will not change any of the members. It was already illegal to change ci, but for this method it is also illegal to change anInt.
It also means that it will not call any non-const methods. So its illegal to call method() from const_method(). It is legal to call const_method_2()

Hope this helps.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
>>c_class (const c_class& Var)
>>  I know that it is a copy constructor... But why the parameter is declared as const?.

That means that the "Var" parameter must be treated as a constant by the procedure that it is passed to (the copy constructor procedure in this case, but other procedures can have const parameters too)  This means that any attempt to change the Var parameter will be reported as an error by the compiler.

There are important reasons for doing this.  The "Var" parameter is being passed by reference, not by value, (it must be passed by reference for the copy constructor, but other procedures may take parameters by value)  If the procedure were to change "Var" the change would be made to the variable referenced.  That means that the caller would "see" the changes made to "Var".  That has some important consequences.  If the caller has a constant value, that is, one that is declared constant, then it could not pass that value here because this procedure would be allowed to change that value, and changing a constant is not allowed.  Similarly, if a unnamed temporary object is created by the compiler, or by the programmer, like by converting one value to another, that unnamed temporary could not be passed because the changes made by this procedure would be lost when the temporary was destroyed.  So by declaring the reference parameter as const, you give the caller more freedom in terms of what can be passed to the procedure.  It is possible for the caller to pass consant value or temporary values.
MarscAuthor Commented:
Thanks KangaRoo...
I guess you are from Australia :-)
Your comment was an answer for me...

And thanks to nietod for your colaboration... I'd like to give you some points, but I have to save some of them... The book I'm reading... is about to start with MFC... I will need many points for that...

I have to say again that I find all the answers here.
Hoppy to help you ;)
just happy to help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.