一个类的常量成员 [英] Constant member of a class

查看:69
本文介绍了一个类的常量成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include< iostream>

使用命名空间std;


class ConstantMember

{

const int m_const;

public:

ConstantMember(int cons = 10):m_const(cons){}

int getConst() const {return m_const;}

};


int main()

{

ConstantMember X; //(20);

cout<< X.getConst();

返回0;

}


以上是编译过的程序&使用g ++链接。是否有任何其他方法在创建对象时使用

构造函数初始化常量成员变量?

#include <iostream>
using namespace std;

class ConstantMember
{
const int m_const;
public:
ConstantMember(int cons = 10):m_const(cons){}
int getConst() const{ return m_const;}
};

int main()
{
ConstantMember X;//(20);
cout << X.getConst();
return 0;
}

the above is a program that was compiled & linked using g++. Is there
any other way to initialise a constant member variable using
constructor while creating objects?

推荐答案

On Fri,2008年7月4日02:08:13 -0700,Anarki写道:
On Fri, 04 Jul 2008 02:08:13 -0700, Anarki wrote:

#include< iostream>

使用命名空间std;


类ConstantMember

{

const int m_const;

public:

ConstantMember(int cons = 10):m_const(cons){} int getConst()const
#include <iostream>
using namespace std;

class ConstantMember
{
const int m_const;
public:
ConstantMember(int cons = 10):m_const(cons){} int getConst() const



{

{


返回m_const;}

};


int main()

{

ConstantMember X; //(20);

cout<< X.getConst();

返回0;

}


以上是编译过的程序&使用g ++链接。有没有

在创建对象时使用构造函数

初始化常量成员变量的任何其他方法?
return m_const;}
};

int main()
{
ConstantMember X;//(20);
cout << X.getConst();
return 0;
}

the above is a program that was compiled & linked using g++. Is there
any other way to initialise a constant member variable using constructor
while creating objects?



有两点:初始化和构造函数体执行,初始化器中初始化const成员函数的原因是

/>
到构造函数体执行时,初始化是完全的,而对于const成员,这是不可能的,因为一旦你

赋值之后你不能改变它的价值。


HTH

There are two points: initialization and constructor body execution, the
reason for const member functions to be initialized in the initializer is
that by the time the constructor body is executed, the initialization is
complete, and for const members, this is not possible because once you
assign the value you can not change it afterwards.

HTH



那里有两点:初始化和构造函数体执行,

在初始化程序中初始化const成员函数的原因
There are two points: initialization and constructor body execution, the
reason for const member functions to be initialized in the initializer



当然 const成员不是const成员函数

Of course "const members" not const member functions


7月4日上午5:08,Anarki< Deepchan ... @ gmail.comwrote:
On Jul 4, 5:08 am, Anarki <Deepchan...@gmail.comwrote:

#include< iostream>

使用命名空间std;


class ConstantMember

{

const int m_const;

public:

ConstantMember(int cons = 10):m_const(cons){}

int getConst()const {return m_const;}

};


int main()

{

ConstantMember X; //(20);

cout<< X.getConst();

返回0;


}


以上是编译的程序&安培;使用g ++链接。是否有

在创建对象时使用

构造函数初始化常量成员变量的任何其他方法?
#include <iostream>
using namespace std;

class ConstantMember
{
const int m_const;
public:
ConstantMember(int cons = 10):m_const(cons){}
int getConst() const{ return m_const;}

};

int main()
{
ConstantMember X;//(20);
cout << X.getConst();
return 0;

}

the above is a program that was compiled & linked using g++. Is there
any other way to initialise a constant member variable using
constructor while creating objects?



是的,在上面的代码中为你生成的复制ctor和

应该看起来像......


class C

{

const int m;

public:

C (int n = 10):m(n){}

C(const C& copy):m(copy.m)// ...这个
{

std :: cout<< " copy \\\
";

}

int get()const {return m; }

};


int main()

{

C c;

std :: cout<< c.get()<< std :: endl;

C cop(c);

std :: cout<< cop.get()<< std :: endl;

}


/ *

10

copy

10

* /


well yes, copy ctor which is generated for you in the code above and
should look something like ...

class C
{
const int m;
public:
C(int n = 10) : m(n) { }
C(const C& copy) : m(copy.m) // ... this
{
std::cout << "copy\n";
}
int get() const { return m; }
};

int main()
{
C c;
std::cout << c.get() << std::endl;
C cop(c);
std::cout << cop.get() << std::endl;
}

/*
10
copy
10
*/


这篇关于一个类的常量成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆