C ++,Classes,Const和奇怪的语法 [英] C++, Classes, Const, and strange syntax

查看:162
本文介绍了C ++,Classes,Const和奇怪的语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我正在重新阅读c ++的引文(第4版) - 成员函数和const引用等部分,我想出了这个小小的程序:

 使用std :: cout; 
using std :: endl;

类ConstCheater
{
public:
ConstCheater(int avalue):ccp(this),value(avalue){}
ConstCheater& getccp()const {return * ccp;}
int value;
private:
ConstCheater * ccp;
};

int main()
{
const ConstCheater cc(7); //将值初始化为7
cout<< cc.value<< endl;
cc.getccp()。value = 4; //现在将其设置为4,即使它是常量!
cout<< cc.value<< endl;
cc.value = 4; //这是非法的
return 0;我的问题是 - 为什么c + +允许像这样的语法????
}


$ b <为什么在声明const时可以在类中编辑普通数据成员?是不是const的POINT使它不能修改值?

解决方案



把他说得更清楚(至少对我来说):

p>

有两个潜在的误解地方:


  1. li>
  2. 在构造const对象时会解释






1。隐式常量



隐式常量( ConstCheater 内部的构造 const )不会将 cc 变为指针到const ,而是 const-pointer ,也就是说当你这样做:

  const ConstCheater cc(7); 

内部来自:

  ConstCheater * ccp; 

...至...

  ConstCheater * const ccp; 

...而不是...

  const ConstCheater * ccp; 



2。构建 const 对象



陌生人的事情是 this 允许在构造函数中传递给 cpp 的初始化器,因为 this 被视为指针到const ,因此不是传递给 const指针的有效值。



也就是说,人们可能会期望:

  ... :ccp(this)... //预期失败,但不会

预期这(等同于):

  const ConstCheater cc(7); 
const ConstCheater * const this =& cc; // const-pointer-to-const

因此你会认为:

  ConstCheater * const ccp = this; // expected error! 

会失败!但它不是因为显然在建设期间显然这个被特别处理,如果它是:

  const ConstCheater * this =& cc; 

,因此对象在构造期间实际上不是const。



我不确定我是否完全理解推理,但是Michael Burr指出,提供预期行为似乎有一个逻辑和技术障碍,所以标准似乎挖出了目前有些奇怪的行为。 p>

我最近问了一个相关问题,即:为什么C ++没有const构造函数?,但是迄今为止还没有真正完全理解为什么它是不可能的,虽然我想这将给C ++开发人员带来一个负担,为任何想要创建const对象的类定义一个笨拙的const构造函数。


I was re-reading c++ primer(4th ed.) today - the section on member functions and const references etc, and I came up with this wierd little program:

using std::cout;
using std::endl;

class ConstCheater
{
public:
    ConstCheater(int avalue) : ccp(this), value(avalue) {}
    ConstCheater& getccp() const {return *ccp;}
    int value;
private:
    ConstCheater* ccp;
};

int main()
{
    const ConstCheater cc(7); //Initialize the value to 7
    cout << cc.value << endl;
    cc.getccp().value = 4;    //Now setting it to 4, even though it's const!
    cout << cc.value << endl;
    cc.value = 4;             //This is illegal
    return 0;
}

My question is - why does c++ allow syntax such as this? Why can I edit normal data members in a class when it's declared const? Isn't the POINT of const to make it so that you can't modify values?

解决方案

I'd say the answer by Tony that you marked as correct is incorrect, and the answer by Michael Burr is correct.

To put what he said more clearly (for me at least):

There are two potential places for misunderstanding:

  1. The way implicit const works
  2. The way that this is interpreted during construction of a const object


1. Implicit Const

Implicit const (the constification internal to ConstCheater when it is made const) doesn't turn cc into a pointer-to-const but rather a const-pointer, that is to say when you do this:

  const ConstCheater cc(7); 

Internally goes from:

  ConstCheater * ccp;

...to...

  ConstCheater * const ccp;

...rather than the...

  const ConstCheater * ccp;    

that may have been expected.

2. Construction of const object

The stranger thing is though is that this is allowed to be passed to cpp's initializer in the constructor since this, one would think, should be treated as a pointer-to-const, and thus not a valid value to pass to a const-pointer.

That is to say one might expect:

 ...: ccp(this) ... // expected to fail but doesnt

to fail because conceptually you might expect that this was (somewhat) equivalent to:

 const ConstCheater         cc(7);
 const ConstCheater * const this = &cc; // const-pointer-to-const

and thus you would think that:

 ConstCheater * const ccp = this; //expected error!

would fail! But it doesn't because apparently during construction apparently this is treated specially as if it was:

 const ConstCheater * this = &cc; 

and thus the object is effectively not const during construction.

I'm not sure I understand completely the reasoning, but Michael Burr points out there appears to be a logical and technical barrier to providing the expected behavior so the standard seems to carve out the current somewhat odd behavior.

I recently asked a related question which was: Why does C++ not have a const constructor? but thus far haven't really understood completely the reasoning why it would be untenable, though I suppose it would place a burden on C++ developers to have to define an awkward const constructor for any class they'd like to create const object of.

这篇关于C ++,Classes,Const和奇怪的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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