How do I define a const member variable in a C + + header file?
------------------------------------------------------------------------------------
A const member variable must be initialized in the constructor.
[Email protected]:~/project/test/const-test/const4_cpp$
[Email protected]:~/project/test/const-test/const4_cpp$ cat Const.h-n
1 #ifndef const_h
2 #define Const_h
3
4 #include <iostream>
5 using Std::cout;
6 using Std::endl;
7
8 Class testtype{
9 const int M_const;
Public:
Testtype (int dat = 0) {}
void Show () const{
cout << "this=" << this << ",";
cout << "M_const: (" << &m_const << "," << m_const << ") \ n";
15}
16};
17
#endif//#ifndef Const_h
[Email protected]:~/project/test/const-test/const4_cpp$ cat Main.cpp-n
1 #include "const.h"
2
3 int main () {
4 cout << __func__ << Endl;
5 Testtype obj (4);
6 obj. Show ();
7}
[Email protected]:~/project/test/const-test/const4_cpp$ g++ main.cpp
In file included from main.cpp:1:0:
Const.h:in constructor ' testtype::testtype (int) ':
Const.h:11:5: error:uninitialized member ' testtype::m_const ' with ' const ' type ' const int ' [-fpermissive]
Testtype (int dat = 0) {}
^
------------------------------------------------------------------------------------
Second, the const member variable cannot be initialized in the constructor body.
[Email protected]:~/project/test/const-test/const4_cpp$ cat Const.h-n
1 #ifndef const_h
2 #define Const_h
3
4 #include <iostream>
5 using Std::cout;
6 using Std::endl;
7
8 Class testtype{
9 const int M_const;
Public:
Testtype (int dat = 0) {m_const = dat;}
void Show () const{
cout << "this=" << this << ",";
cout << "M_const: (" << &m_const << "," << m_const << ") \ n";
15}
16};
17
#endif//#ifndef Const_h
[Email protected]:~/project/test/const-test/const4_cpp$ cat Main.cpp-n
1 #include "const.h"
2
3 int main () {
4 cout << __func__ << Endl;
5 Testtype obj (4);
6 obj. Show ();
7}
[Email protected]:~/project/test/const-test/const4_cpp$ g++ main.cpp
In file included from main.cpp:1:0:
Const.h:in constructor ' testtype::testtype (int) ':
Const.h:11:5: error:uninitialized member ' testtype::m_const ' with ' const ' type ' const int ' [-fpermissive]
Testtype (int dat = 0) {m_const = dat;}
^
Const.h:11:37:error:assignment of Read-only member ' Testtype::m_const '
Testtype (int dat = 0) {m_const = dat;}
^
[Email protected]:~/project/test/const-test/const4_cpp$
------------------------------------------------------------------------------------
The const member variable can only be initialized in the constructor initialization list. Each object has a m_const.
[Email protected]:~/project/test/const-test/const4_cpp$ cat Const.h-n
1 #ifndef const_h
2 #define Const_h
3
4 #include <iostream>
5 using Std::cout;
6 using Std::endl;
7
8 Class testtype{
9 const int M_const;
Public:
Testtype (int dat = 0): m_const (DAT) {}
void Show () const{
cout << "this=" << this << ",";
cout << "M_const: (" << &m_const << "," << m_const << ") \ n";
15}
16};
17
#endif//#ifndef Const_h
[Email protected]:~/project/test/const-test/const4_cpp$ cat Main.cpp-n
1 #include "const.h"
2
3 int main () {
4 cout << __func__ << Endl;
5 Testtype obj (4);
6 obj. Show ();
7}
[Email protected]:~/project/test/const-test/const4_cpp$ g++ main.cpp
[Email protected]:~/project/test/const-test/const4_cpp$./a.out
Main
this=0x7fff7ab7d960, M_const: (0x7fff7ab7d960, 4)
[Email protected]:~/project/test/const-test/const4_cpp$
------------------------------------------------------------------------------------
This article from the "write poetry with C + +" blog, declined reprint!
How do I define a const member variable in a C + + header file?