指向基类的成员指针

发布于 2024-08-20 06:19:51 字数 351 浏览 5 评论 0原文

全部。我无法理解为什么下面的代码需要强制转换才能工作。有人可以解释一下吗?

class Base {
};

class Derived : public Base {
};

class Class {
public:
    Derived member;
};

...

Derived obj;
Base *ptrObj = &obj; // ok, no cast needed

Derived Class::* ptr = &Class::member; // ok
Base    Class::* ptr = &Class::member; // wrong, need cast, why?

all. I can't undestand why the bellow code need a cast to work. Someone can explain it?

class Base {
};

class Derived : public Base {
};

class Class {
public:
    Derived member;
};

...

Derived obj;
Base *ptrObj = &obj; // ok, no cast needed

Derived Class::* ptr = &Class::member; // ok
Base    Class::* ptr = &Class::member; // wrong, need cast, why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

栀梦 2024-08-27 06:19:51

因为如果允许Base(协变),那么你就可以这样做,这是一个禁忌:

Base Class::* ptr = &Class::member;
Class obj;
obj.*ptr = Base();   // <-- assigned a Base into a Derived field?!

同时,指向成员的指针也不能是逆变的,因为否则你可以这样做,这也是一个禁忌:

struct Class2 {
    Base member;
};

Derived Class2::* ptr2 = &Class2::member;
Class2 obj2;
obj2.member = Base();
Derived& d = obj2.*ptr2;  // <-- assigned a Base into a Derived

因此,指向成员的指针既不是协变也不是逆变,而是不变的:类型必须完全匹配。

Because if Base were allowed (covariant), you could then do this, which is a no-no:

Base Class::* ptr = &Class::member;
Class obj;
obj.*ptr = Base();   // <-- assigned a Base into a Derived field?!

At the same time, pointers-to-members cannot be contravariant either, because otherwise you could do this, which is also a no-no:

struct Class2 {
    Base member;
};

Derived Class2::* ptr2 = &Class2::member;
Class2 obj2;
obj2.member = Base();
Derived& d = obj2.*ptr2;  // <-- assigned a Base into a Derived

So, pointers-to-members are neither covariant nor contravariant, but are invariant: the type must match exactly.

奈何桥上唱咆哮 2024-08-27 06:19:51

好吧,我明白你的意思了,克里斯,但是你的第一个例子适用于普通指针。为什么它不适用于成员指针?请参阅下面的代码。

Derived obj;
Base *ptr = &obj;

*ptr = Base(); // it's weird, but ok

第二个示例即使对于普通指针也不起作用,因为不进行强制转换就不允许向下转换。所以我认为这不应该是一个解释。

Ok, I got your point Chris, but your first example works for ordinary pointers. Why should it not work for member pointers too? See the code bellow.

Derived obj;
Base *ptr = &obj;

*ptr = Base(); // it's weird, but ok

The second example will not work even for ordinary pointers, since downcasting is not allowed without cast. So I don't think that should be a explanation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文