How to fix this:
Code:class ABC { private: const int a = 5; };
This is a discussion on Const class members within the C++ Programming forums, part of the General Programming Boards category; How to fix this: Code: class ABC { private: const int a = 5; };...
How to fix this:
Code:class ABC { private: const int a = 5; };
Use an initialisation in the constructors.
Code:class ABC { public: ABC() : a(5) {} void func() { cout << a << endl; } private: const int a; }; int main ( ) { ABC foo; foo.func(); return 0; }
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I support http://www.ukip.org/ as the first necessary step to a free Europe.
If you mean how to initialize const data members, the easiest way would be to create the constructor and use the initialization list. See this link.
Jim
Solved, tanks for replying.