多重继承和鸭子打字

Mat*_*tor 2 c++

在使用Kira3时,我正在使用C++编译器并寻找实现Kira的鸭子打字的好方法.我希望(因为已经有几年的直接C++编程)我可以在多种类型下使用多重继承来进行成员访问.唉,到目前为止我失败了......

理想的代码如下:

class WithX { public: int x; };
class WithY { public: int y; };
class WithZ { public: int z; };

class Point2D : public WithX, public WithY { };
class Point3D : public WithZ, public WithX, public WithY { };

void ZeroOut(Point2D * p) { p->x = 0; p->y = 0; };

int _tmain(int argc, _TCHAR* argv[])
{
    Point3D* p = new Point3D();
    p->x = 1;
    p->y = 1;
    p->z = 1;
    ZeroOut(p);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是,它会在调用ZeroOut(p)时抛出输入错误.这是超悲伤的表情.我可以通过创建一个类型塔来强制它工作.在上面的例子中,我可以更改类Point3D:public WithZ,public WithX,public WithY {}; to class Point3D:public Point2D,public WithZ {};

现在的问题是我有重叠的结构.我要么在kira代码中抛出一个很难解决的错误,要么让编译器做一些不同的事情.我试过了

class Point3D : public Point2D, public WithZ, public WithX, public WithY { }; 
Run Code Online (Sandbox Code Playgroud)

希望编译器能够将它们组合在一起,但这会给成员变量带来模糊的访问.这可以通过复制对不明确的成员变量的写入来解决,这可能是我编译阶段的解决方案.此解决方案需要更多内存.

任何想法如何解决这个没有记忆丧失?

或者,有没有办法将变量转换为多种类型?喜欢

(WithX,WithY *)p
Run Code Online (Sandbox Code Playgroud)

Mar*_*off 8

我对Kira一无所知,但你可以用模板函数解决至少这个特殊问题- 这有点像编译时鸭子打字,在某种程度上.

#include <iostream>
class WithX { public: int x; };
class WithY { public: int y; };
class WithZ { public: int z; };

class Point2D : public WithX, public WithY { };
class Point3D : public WithZ, public WithX, public WithY { };

// As long as we pass in a type that has an x and a y member,
// ZeroOut will compile and run correctly, setting x and y to zero.
template <typename T>  
void ZeroOut(T * p) { p->x = 0; p->y = 0; };

int main(int argc, char* argv[])
{
    Point3D* p = new Point3D();
    p->x = 1;
    p->y = 1;
    p->z = 1;
    ZeroOut(p);
    std::cout << p->x << " " << p->y << " " << p->z << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

正确输出:

$ ./a.out
0 0 1

您在评论中提到使用更多代码 - 我想指出添加的代码可以忽略不计.我把主要功能改为:

int main(int argc, char* argv[])
{
    Point3D* p = new Point3D();
    p->x = 1;
    p->y = 1;
    p->z = 1;
    ZeroOut(p);
#ifdef USE_2D
    Point2D *q = new Point2D();
    q->x = 1;
    q->y = 1;
    ZeroOut(q);
#endif
    std::cout << p->x << " " << p->y << " " << p->z << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

并以这种方式编译给出:

$ g++ p.cpp -o just3
$ g++ p.cpp -DUSE_2D -o 3and2
$ wc -c *3*
 9949 3and2
 9908 just3
19857 total

你真的要在可执行文件中超过41字节的差异吗?好吧,如果你是,只需打开优化:

$ g++ -O2 p.cpp -o just3
$ g++ -O2 p.cpp -DUSE_2D -o 3and2
$ wc -c *3*
 9882 3and2
 9882 just3
19764 total