gpt4 book ai didi

c++ - 转换运算符正在切割我的对象

转载 作者:搜寻专家 更新时间:2023-10-31 00:16:16 26 4
gpt4 key购买 nike

我从以下代码中得到了意外的行为:

struct Base
{
Base() {}
virtual ~Base() {}

virtual void foo() const = 0;

protected:
Base(const Base &) {}
};

struct Derived : public Base
{
Derived() {}
Derived(const Derived &other) : Base(other) {}

virtual void foo() const {}
};

struct NewDerived
{
operator const Derived() { return Derived(); }
};

void func(const Base &b)
{
b.foo();
}

int main()
{
func(NewDerived());
return 0;
}

使用 MSVC2008,我在 main() 中遇到此编译错误:

error C2248: 'Base::Base' : cannot access protected member declared in class 'Base'

为什么要访问 Base 的拷贝构造函数?

如果我公开 Base 的复制构造函数,代码会在运行时编译和切片返回值,并且在 func() 中调用 foo() 会触发一个名为 error 的纯虚函数。

有人可以透露一点信息吗?

最佳答案

标准的相关引用在 8.5.3p5 (C++11) 中:

has a class type (i.e., T2 is a class type), where T1 is not reference-related to T2, and can be implicitly converted to an xvalue, class prvalue, or function lvalue of type “cv3 T3”, where “cv1 T1” is reference-compatible with “cv3 T3”, then the reference is bound to the value of the initializer expression in the first case and to the result of the conversion in the second case (or, in either case, to an appropriate base class subobject).

Example:

struct A { };
struct B : A { } b;
extern B f();
const A& rca2 = f(); // bound to the A subobject of the B rvalue.
A&& rra = f(); // same as above
struct X {
operator B();
operator int&();
} x;
const A& r = x; // bound to the A subobject of the result of the conversion

在你的例子中,T1BaseT2NewDerivedT3Derived。根据上面的引述,复制构造函数不应该被调用,左值引用应该绑定(bind)到Base子对象。

但是请注意,在 C++03 中,情况并非如此。在 C++03 中,以下引用是相关的:

If the initializer expression is an rvalue, with T2 a class type, and “cv1 T1” is reference-compatible with “cv2 T2,” the reference is bound to the object represented by the rvalue (see 3.10 [basic.lval]) or to a sub-object within that object.

...

Otherwise, a temporary of type “cv1 T1” is created and initialized from the initializer expression using the rules for a non-reference copy initialization (8.5 [dcl.init]). The reference is then bound to the temporary.

引用的第一段不适用,因为 BaseNewDerived 的引用不兼容,所以只有最后一段适用,这意味着临时 Base 必须创建对象。因此,MSVC2008和gcc是符合C++03规则的。

关于c++ - 转换运算符正在切割我的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16723208/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com