More about const

Posted on 1999-10-27
Medium Priority
Last Modified: 2011-10-03
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...

Question by:Marsc
  • 2
  • 2

Accepted Solution

KangaRoo earned 160 total points
ID: 2161728
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(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(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.
LVL 22

Expert Comment

ID: 2162181
>>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.

Author Comment

ID: 2164215
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.

Expert Comment

ID: 2165023
Hoppy to help you ;)
LVL 22

Expert Comment

ID: 2165458
just happy to help.

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

592 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question