How do I define a const member variable in a C + + header file?

Source: Internet
Author: User

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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.