的const_cast< ...>(本) [英] const_cast<...>(this)
问题描述
class my_class {
public:
my_class():value(0){}
int& get_value(){返回值; }
const int& get_value()const {
my_class& c = const_cast< my_class&>(* this);
返回c.get_value();
}
私人:
int value;
};
int main(){
const my_class c ;
返回c.get_value();
}
以上是否导致未定义的行为?
谢谢
class my_class {
public:
my_class() : value(0) { }
int& get_value() { return value; }
const int& get_value() const {
my_class& c = const_cast<my_class&>(*this);
return c.get_value();
}
private:
int value;
};
int main() {
const my_class c;
return c.get_value();
}
Does the above cause undefined behavior?
Thanks
推荐答案
* Squeamizh:
* Squeamizh:
class my_class {
public:
my_class():value(0){}
int& get_value(){返回值; }
const int& get_value()const {
my_class& c = const_cast< my_class&>(* this);
返回c.get_value();
}
私人:
int value;
};
int main(){
const my_class c ;
返回c.get_value();
}
以上是否导致未定义的行为?
class my_class {
public:
my_class() : value(0) { }
int& get_value() { return value; }
const int& get_value() const {
my_class& c = const_cast<my_class&>(*this);
return c.get_value();
}
private:
int value;
};
int main() {
const my_class c;
return c.get_value();
}
Does the above cause undefined behavior?
不,但是(1)如果你试图修改对象,那就是因为它是'/ b $ b b最初声明''const '',和(2)而不是上面的怪物,
,你通过荒谬的代码完全暴露数据成员,你可以和
可能应该只是做
struct MyClass
{
int value;
MyClass():value(0){}
};
如果你绝对想要使用类似Java的getter (它的目的是在
Java中,即通过instrospection支持组件使用)然后我建议你将名称''get_value'改为''value' '。
你不会写
get_sin(0.5)* get_cos(0.5)
你呢?
-
答:因为它弄乱了人们通常阅读文字的顺序。
问:为什么这么糟糕?
A:热门发布。
问:usenet和电子邮件中最烦人的是什么?
No, but (1) it would if you tried to modify the object, because it''s
originally declared ''const'', and (2) instead of the monstrosity above,
where you expose a data member completely via absurd code, you could and
probably should simply do
struct MyClass
{
int value;
MyClass(): value( 0 ) {}
};
If you absolutely want to use a Java-like "getter" (it has a purpose in
Java, namely to support component usage via instrospection) then I
suggest you change the name ''get_value'' to simply ''value''.
You don''t write
get_sin(0.5)*get_cos(0.5)
do you?
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Alf P. Steinbach写道:
Alf P. Steinbach wrote:
[const_cast< ... >(this)]
以上是否导致未定义的行为?
[const_cast<...>(this)]
Does the above cause undefined behavior?
不,但是(1)如果你试图修改对象,那就是因为它是'/ b $ b最初声明为''const '',和(2)而不是上面的怪物,
,你通过荒谬的代码完全暴露数据成员,你可以和
可能应该只是做
[struct]
No, but (1) it would if you tried to modify the object, because it''s
originally declared ''const'', and (2) instead of the monstrosity above,
where you expose a data member completely via absurd code, you could and
probably should simply do
[struct]
我同意。我发布的代码是简化的,以便清楚地表达我关心的根源。但是,自从你提出来之后,如果你认为我使用了错误的解决方案来解决我的问题,我会感激不尽的。
感激不尽。我最初有一个类声明,如下所示:
class my_class {
public:
virtual int& get_value()= 0;
虚拟const int& get_value()const = 0;
};
my_class实际上并不包含int value (因此纯粹的
虚拟吸气剂)因为我希望有一个my_class的后代
(class outer),它包含my_class的另一个后代(" class)
inner")。 inner包含实际的int value,而外部只是
将上述两个函数委托给inner。但是,我不希望
外部必须覆盖非const版本的get_value和
const版本的get_value。我想避免在所有这些课程中覆盖两倍的吸气剂的单调乏味。
这可能看起来仍然很荒谬,但它仍然有点简化。
实际上是上面的八个左右的吸气剂,还有几个不同的
外部和内在的所有继承my_class的类。
I agree. The code I posted was simplified in order to clearly express
the root of my concern. Since you brought it up, though, I''d be
grateful to hear if you think I am using the wrong solution for my
problem. I originally had a class declaration that looks like this:
class my_class {
public:
virtual int& get_value() = 0;
virtual const int& get_value() const = 0;
};
my_class does not actually contain an "int value" (hence the pure
virtual getter) because I would like to have a descendant of my_class
("class outer") that contains another descendant of my_class ("class
inner"). inner contains the actual "int value", and outer just
delegates the above two functions to inner. However, I don''t want
outer to have to override both a non-const version of get_value and a
const version of get_value. I would like to avoid the tediousness of
overriding twice as many getters in all these classes.
This may still seem absurd, but it is still a bit simplified. There
are actually eight or so getters like the above, and several different
"outer" and "inner" classes that all inherit my_class.
如果你绝对想要使用类似Java的getter (它的目的是在
Java中,即通过instrospection支持组件使用)然后我建议你将名称''get_value'改为''value' '。
你不会写
get_sin(0.5)* get_cos(0.5)
你呢?
If you absolutely want to use a Java-like "getter" (it has a purpose in
Java, namely to support component usage via instrospection) then I
suggest you change the name ''get_value'' to simply ''value''.
You don''t write
get_sin(0.5)*get_cos(0.5)
do you?
再次,我同意。我经常很难阅读其他人发布的片段,我认为增加了获取。会更容易
来阅读我的例子。
Again, I agree. I often have difficulty reading the snippets that
others post, and I thought the addition of "get" would make it easier
to read my example.
Alf P. Steinbach写道:
Alf P. Steinbach wrote:
* Squeamizh:
* Squeamizh:
> class my_class {
public:
my_class():value(0) {}
int& get_value(){返回值; }
const int& get_value()const {
my_class& c = const_cast< my_class&>(* this);
返回c.get_value();
}
私人:
int value;
};
int main(){
const my_class c;
返回c.get_value();
}
以上是否导致未定义的行为?
>class my_class {
public:
my_class() : value(0) { }
int& get_value() { return value; }
const int& get_value() const {
my_class& c = const_cast<my_class&>(*this);
return c.get_value();
}
private:
int value;
};
int main() {
const my_class c;
return c.get_value();
}
Does the above cause undefined behavior?
不,但是(1)如果你试图修改对象,那就是因为它是'/ b $ b最初声明为''const '',
No, but (1) it would if you tried to modify the object, because it''s
originally declared ''const'',
编译器会在尝试时咆哮:
const int& get_value()const {
my_class& c = const_cast< my_class&>(* this);
返回c.get_value();
}
As你可以看到,get_value的const版本返回一个const int&。
The compiler would bark at the attempt:
const int& get_value() const {
my_class& c = const_cast<my_class&>(*this);
return c.get_value();
}
As you can see, the const version of get_value returns a const int &.
和(2)而不是上面的怪物,
在那里你通过荒谬的代码完全暴露数据成员,你可以和
可能应该简单地做
struct MyClass
{
int value;
MyClass():value(0){}
};
如果你绝对想要使用类似Java的getter (它的目的是在
Java中,即通过instrospection支持组件使用)然后我建议你将名称''get_value'改为''value' '。
你不会写
get_sin(0.5)* get_cos(0.5)
你呢?
and (2) instead of the monstrosity above,
where you expose a data member completely via absurd code, you could and
probably should simply do
struct MyClass
{
int value;
MyClass(): value( 0 ) {}
};
If you absolutely want to use a Java-like "getter" (it has a purpose in
Java, namely to support component usage via instrospection) then I
suggest you change the name ''get_value'' to simply ''value''.
You don''t write
get_sin(0.5)*get_cos(0.5)
do you?
我认为OP对特定的例子并不感兴趣。相反,
我认为代码是试图避免代码重复。更自然的
示例将是一个智能指针,你有
T *操作符*(无效);
和
T const * operator *(void)const;
且这两个函数通常具有相同的主体。我不知道该怎么想
的const_cast<方法。似乎除非它是你的同伴群体中已经建立的成语,否则你必须输入一个
的评论说你检查了标准并且这个用途
const_cast<是良性的---几乎不仅仅是复制
单行函数体的代码。
Best
Kai-Uwe Bux
I think the OP is not really interested in the particular example. Instead,
I think the code is an attempt at avoiding code-duplication. A more natural
example would be a smart-pointer where you have
T* operator* ( void );
and
T const * operator* ( void ) const;
and both functions usually have the same body. I am not sure what to think
of the const_cast<approach, though. It seems that unless it is an
established idiom within your peer group, you would have to put in a
comment saying that you checked the standard and that this use of
const_cast<is benign---hardly better than just duplicating the code of a
one-line function body.
Best
Kai-Uwe Bux
这篇关于的const_cast< ...>(本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!