c++ - 错误 : invalid conversion from 'bottom**' to 'lefta**'

标签 c++

我正在尝试编译并运行这个程序。显然,它不起作用!我的问题是为什么从 bottom** 到 lefta** 的转换无效,而 bottom* 可以转换为 lefta* ?

#include<iostream>
using namespace std;
class top
{
private:
    int a;
public:
    top(int b):a(b){}
    virtual void output(){cout<<a<<endl;}
};
class lefta:virtual public top
{
private:
    int b;
public:
    lefta(int c,int d):top(c),b(d){}
    void output(){cout<<b<<endl;}
};
class righta:virtual public top
{
private:
    int c;
public:
    righta(int c,int d):top(c),c(d){}
    void output(){cout<<c<<endl;}
};
class bottom:public lefta,public righta
{
private:
    int d;
public:
    bottom(int e,int f,int g,int h):top(e),lefta(e,f),righta(e,g),d(h){}
    void output(){cout<<d<<endl;}
};
int main()
{
    bottom* bo=new bottom(1,2,3,4);
//  lefta* le=bo;
//  le->output();
    bottom** p_bo=&bo;//here
    lefta** p_le=p_bo;//here
    (*p_le)->output();
    return 0;
 }

最佳答案

class leftb : public lefta { /* blah */ };

bottom* bo = new bottom(1,2,3,4);
bottom** p_bo = &bo;
lefta** p_le = p_bo;// let's pretend it works
// now p_le points to the variable bo, which is of type bottom*
// so *p_le is a reference to the variable bo
*p_le = new leftb(1,2); // wait, did we just assign a leftb* to a bottom*?
// (and yeah, I'm leaking memory. Sue me)
// bo now points to a leftb, but it is a bottom*
// oops, we just broke the type system

关于c++ - 错误 : invalid conversion from 'bottom**' to 'lefta**' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9397462/

相关文章:

c++ - std::vector 转换运算符的 C2664

C++:指示一个函数可能会抛出

无符号整数中的 C++ 按位补码返回负值

c++ - 如何避免NRVO的 "pessimizing-move"警告?

c++ - Mongodb C++ API 插入二进制文件(图片)

c++ - 我如何在 Qt 中将 foreach 与 QDomNodeList 一起使用?

c# - 项目引用另一个项目的 obj 文件夹中的 dll - 有时编译有时不编译

C++如何对齐文件的每一行?

c# - Visual Studio 2017 在 64 位系统上以 32 位编译?

c++ - 工厂 : how to pass temporary smart pointers to functions. C++