this 指针上的 Const_cast - 我会被其他 C++ 告知吗?编码员这样做?
我有一个类 Game
例如
class Game
{
public:
InitObjects();
...
};
,我有另一个类 Grid
,需要使用对该 Game
对象的非常量引用进行初始化。 (Grid
对象需要调用可以更新Game
对象的函数)。
class Grid
{
public:
Grid(Game & g):
game(g){}
...
private:
Game & game;
...
};
Game
对象负责初始化Grid
。我这样做了:
void Game::InitObjects()
{
grid = new Grid(*(const_cast<Game*>(this)) );
}
grid
不是 Game
的成员 - 它是一个全局的(啊 - 我知道......我不介意让它成为一个成员,但我有同样的问题吗?)。 一些经验丰富的 C++ 人员可以告诉我这种奇怪的 const_cast
是否可以接受吗?
I have a class Game
e.g.
class Game
{
public:
InitObjects();
...
};
And I have another class Grid
, that needs to be initialised with a non-const reference to that Game
object. ( A Grid
object needs to call functions that can update a Game
object ).
class Grid
{
public:
Grid(Game & g):
game(g){}
...
private:
Game & game;
...
};
The Game
object is responsible for initialising the Grid
. I did this:
void Game::InitObjects()
{
grid = new Grid(*(const_cast<Game*>(this)) );
}
grid
is not a member of a Game
- it's a global ( argh - i know... I don't mind making it a member, but I have the same problem right? ).
Can some seasoned C++ folk tell me if this odd looking const_cast
is acceptable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
const_cast 的主要问题是它违反了不会对对象造成任何更改的承诺。它主要用于与模块(C 模块?)接口,其中 const 的使用不一致,您应该验证是否有任何更改。
另一种选择可能是拥有一些可变成员,但当然,这些成员应该是设计上可变的(如锁、缓存),而不是为了方便(嘿,当对象应该是 const 时,我想更改它)。
你的问题的问题在于你的问题中没有任何东西是 const 的,所以我认为不需要 const_cast 。
The main problem with const_cast is that it violates a promise of not causing any change to the object. It is mainly needed for interfacing with modules (C-modules?) where const isn't used consistently and you are supposed to verify whether there could be any change.
An alternative might be to have some mutable members, but of course, these should be mutable by design (like locks, caches), not by convenience (hey, I want to change this when the object issupposed to be const).
The problem with your question is that nothing is const in your question, so I don't see any need for const_cast.
我在没有选角的情况下见过更可怕的事情。
我给出的上面的代码是“我现在处于一个非常量函数中,所以我将获取对自己的非常量引用,这样即使我处于常量上下文中,我也可以稍后在需要时更改对象”。这实际上比你可能的场景(如果 createObjects() 是 const )更糟糕,即“这个函数是 const 因为它现在不改变状态,但需要传递一个对 self 的非常量引用,因为我的状态将会改变之后”。
当然,首先比 const 修饰符更好的是分割接口(对象的可变接口派生自非可变接口)。
我上面看到的是 Game 和 Grid 之间的一对一关系。网格具有对一个游戏的引用,并且游戏的 initObjects 知道一个网格。所以两者的耦合非常紧密。这并不一定意味着它们应该是一个对象 - 再次分割接口 - 你可能想要传递一个 Grid&或游戏&仅供使用该类的接口的参考。
InitObjects 是从构造函数中调用的吗?
I have seen more horrible things in my time without casting at all.
The above code I have given is "I am in a non-const function now so I will grab a non-const reference to myself so I can change the object later when I need to even when I am in a const context". Which is actually worse than your possible scenario (if createObjects() is const) that is saying "This function is const because it is not changing the state now, but needs to pass out a non-const reference to self as my state will change later".
Of course better than the const modifier in the first place would be a split interface (mutable interface to an object derives from non-mutable one).
What I am looking at above is the one-to-one relationship between Game and Grid. Grid has a reference to one Game and Game's initObjects knows of one Grid. So the two are very tightly coupled. That does not necesasrily mean they should be one object - again split interface - you might want to pass a Grid& or a Game& reference for use of the interface of that class only.
Is InitObjects called from the constructor?
鉴于
InitObjects
不是const
,除非您在const Game
实例上调用InitObjects
,否则不需要const_cast
。不过,我会对这种循环依赖有所不同Given
InitObjects
is notconst
, unless you are callingInitObjects
on aconst Game
instance, there is no need for theconst_cast
. I'd be vary of that circular dependency though从对象进行的构造不应写入该对象,因此您不会调用 UB(您可以通过写入通过 const_cast 解构的内容来调用 UB)。然而,这看起来确实很奇怪。特别是因为
Game::InitObjects()
不是const
成员函数。为什么你认为this
指针是const
?!Construction from an object should not write to the object, so you won't be invoking UB (which you would be by writing to something that you de-consted through
const_cast
). However, this does look odd. Especially sinceGame::InitObjects()
is not aconst
member function. Why do you think that thethis
pointer isconst
?!