抛弃常量 [英] Casting Away Constness

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

问题描述

我希望能够在一个类的
a成员函数中抛弃私有成员变量的常量。


我有私有数据成员声明如下:


const double x;


我有一个重载的赋值运算符实现如下:

Point& Point :: operator =(const Point * somePoint)

{

*((double *)& x)= somePoint-> ; x;

}


虽然以上编译,我认为更新的更可接受的方式来实现这一点是使用const_cast ;但是,我似乎不明白

如何实现const_cast。我尝试了以下方法:


const_cast< Point *>(this) - > x = somePoint-> x;


但是这会返回以下编译器错误:


point.cpp:在方法`class Point& Point :: operator =(const Point *)'':

point.cpp:58:只读成员的分配`Point :: x''


有人可以告诉我如何正确使用const_cast来施放

远离constness?在此先感谢(请原谅我已经讨论过这个问题已经讨论过了;我刚刚订阅了这个新闻组,并且尽可能地搜索了我的b $ b,但我不能'没有提到const_cast。

I want to be able to cast away the constness of a private member variable in
a member function of a class.

I have the private data member declared as follows:

const double x;

I have an overloaded assignment operator implemented as follows:

Point &Point::operator=( const Point *somePoint )
{
*( ( double * ) &x ) = somePoint->x;
}

Although the above compiles, I thought the newer more acceptable way to
accomplish this was using const_cast; however, I seem to not understand how
to implement const_cast. I tried the following:

const_cast< Point * >( this )->x = somePoint->x;

But this returns the following compiler error:

point.cpp: In method `class Point & Point::operator =(const Point *)'':
point.cpp:58: assignment of read-only member `Point::x''

Can someone please enlighten me as to the proper use of const_cast to cast
away constness? Thanks in advance (and please excuse me if this has been
discussed already; I just subscribed to this newsgroup and searched as far
back as I could, but I couldn''t find any mention of const_cast).

推荐答案

Trevor Lango写道:
Trevor Lango wrote:
我希望能够抛弃
类成员函数中私有成员变量的常量。


你熟悉可变吗? keword?

我将私有数据成员声明如下:

const double x;


如果你知道要更改它,为什么你声明它为const?

我有一个重载的赋值运算符实现如下:

Point& Point :: operator =(const Point * somePoint)
{
*((double *)& x)= somePoint-> x;
}

虽然以上编译,我认为更新的更可接受的方法来实现这一点是使用const_cast;但是,我似乎不明白如何实现const_cast。我尝试了以下方法:

const_cast< Point *>(this) - > x = somePoint-> x;

但是这会返回以下编译器错误:

point.cpp:在方法`类中点与点Point :: operator =(const Point *)'':
point.cpp:58:只读成员的分配`Point :: x''


你& ; unconsted" Point对象。成员x因为你因此宣布了这个原因所以是const的原因。
有人可以告诉我如何正确使用const_cast来施放
远离constness ?


const_cast< double>(x)= somePoint-> x;


如果你发现自己需要const cast,那么程序中某处就会出现不一致的情况。如果您了解不一致的地方,

并且您可以使用它,您仍然可以将可变成员视为替换演员的替换成员。

提前致谢(如果已经讨论过,请原谅我;我只是订阅了这个新闻组并尽可能地回来了,但我找不到了,但我找不到任何提及const_cast的事。)
I want to be able to cast away the constness of a private member variable in
a member function of a class.
Are you familiar with the "mutable" keword?
I have the private data member declared as follows:

const double x;
Why did you declare it const, if you knew you were going to change it?
I have an overloaded assignment operator implemented as follows:

Point &Point::operator=( const Point *somePoint )
{
*( ( double * ) &x ) = somePoint->x;
}

Although the above compiles, I thought the newer more acceptable way to
accomplish this was using const_cast; however, I seem to not understand how
to implement const_cast. I tried the following:

const_cast< Point * >( this )->x = somePoint->x;

But this returns the following compiler error:

point.cpp: In method `class Point & Point::operator =(const Point *)'':
point.cpp:58: assignment of read-only member `Point::x''
You "unconsted" the Point object. The member "x" was const for a
different reason, namely because you declared it thus.
Can someone please enlighten me as to the proper use of const_cast to cast
away constness?
const_cast< double >( x ) = somePoint->x;

If you find yourself needing const cast, there is an inconsistency
somewhere in your program. If you understand what the inconsistency is,
and you''re OK with it, you still might consider a mutable member as an
alternative to the cast.
Thanks in advance (and please excuse me if this has been
discussed already; I just subscribed to this newsgroup and searched as far
back as I could, but I couldn''t find any mention of const_cast).




欢迎!


-Jeff



Welcome!

-Jeff


" Trevor Lango" < TM ***** @ sbcglobal.net>写道...
"Trevor Lango" <tm*****@sbcglobal.net> wrote...
我希望能够在类的成员函数中抛弃私有成员变量
的常量。

我有私有数据成员声明如下:

const double x;


为什么是const?

我有一个重载的赋值运算符实现如下:

Point& Point :: operator =( const Point * somePoint)


为什么要从指针中分配?一个相当奇怪的任务操作。

好​​吧,不管......

{
*((double *)& x)= somePoint-> x ;


更简单的是


(double&)x = somePoint-> x;

} 如何实现const_cast。我尝试了以下方法:

const_cast< Point *>(this) - > x = somePoint-> x;

但是这会返回以下编译器错误:

point.cpp:在方法`类中点与点Point :: operator =(const Point *)'':
point.cpp:58:只读成员的分配`Point :: x''


你演员离开对象本身的constness,这是(a)不需要因为

对象已经是非const而且(b)不会影响
$ b $的常数ba成员明确声明const。


你_could_做


const_cast< double&>(x)= somePoint-> x;


但是,你仍然必须回答这个问题:你为什么要首先成为会员

''const''?
<有人可以请教我如何正确使用const_cast来施放constness?


我认为你需要一些启发才能正确使用const

尝试使用const_cast ...但这只是我得到的印象。

提前致谢(如果已经讨论过,请原谅我;我刚刚订阅了这个新闻组,并尽可能地搜索到了
,但我不能没有提到const_cast。。
I want to be able to cast away the constness of a private member variable in a member function of a class.

I have the private data member declared as follows:

const double x;
Why const?

I have an overloaded assignment operator implemented as follows:

Point &Point::operator=( const Point *somePoint )
Why are you assigning from a pointer? One rather strange assignment op.
Well, no matter...
{
*( ( double * ) &x ) = somePoint->x;
Simpler would be

(double&) x = somePoint->x;
}

Although the above compiles, I thought the newer more acceptable way to
accomplish this was using const_cast; however, I seem to not understand how to implement const_cast. I tried the following:

const_cast< Point * >( this )->x = somePoint->x;

But this returns the following compiler error:

point.cpp: In method `class Point & Point::operator =(const Point *)'':
point.cpp:58: assignment of read-only member `Point::x''
You cast away constness of the object itself, which is (a) unneeded because
the object is already non-const and (b) doesn''t affect the const-ness of
a member declared explicitly const.

You _could_ do

const_cast<double&>(x) = somePoint->x;

But still, you have to answer this question: why did you make the member
''const'' in the first place?

Can someone please enlighten me as to the proper use of const_cast to cast
away constness?
I think you need some enlightment as to proper use of const before you
attempt using const_cast... But that''s just the impression I get.
Thanks in advance (and please excuse me if this has been
discussed already; I just subscribed to this newsgroup and searched as far
back as I could, but I couldn''t find any mention of const_cast).




抛弃常数并不是你应该随便做的事情。在

大多数情况下,如果你抛弃常量,你必须确保原始对象_was_声明/定义为非const,否则发生UB。


Victor



Casting away const-ness is not what you should be doing casually. In
most cases if you cast away const-ness, you _must_ be sure that the
original object _was_ declared/defined as non-const, otherwise UB occurs.

Victor




" Trevor Lango" < TM ***** @ sbcglobal.net>在留言新闻中写道:6v ***************** @ newssvr27.news.prodigy.co m ...

"Trevor Lango" <tm*****@sbcglobal.net> wrote in message news:6v*****************@newssvr27.news.prodigy.co m...
我希望能够投
const_cast<中的私有成员变量的常量Point *>(this) - > x = somePoint-> x;
I want to be able to cast away the constness of a private member variable in
const_cast< Point * >( this )->x = somePoint->x;



* const_cast< Point *>(& x)= somePoint-> x; < br $>


const_cast< Point&>(x)= somePoint-> x;


*const_cast<Point*>(&x) = somePoint->x;
or
const_cast<Point&>(x) = somePoint->x;


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

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