C++同名运算符的多重继承

发布于 2024-08-17 02:10:24 字数 612 浏览 2 评论 0原文

是否可以从两个不同的抽象类继承仅返回类型不同的同名运算符。 如果是这样,他们:

  • 实现运算符的语法是什么

  • 使用/解析运算符的语法是什么

  • 一般情况下的开销是多少,与任何其他虚函数相同?

如果您可以为我提供参考或示例代码,这将很有帮助,

感谢

12struct abstract_matrix {
 13    virtual double& operator()(int i, int j);
 14};
 15
 16    struct abstract_block_matrix {
 17        virtual double* operator()(int i, int j);
 18    };
 19
 20struct block_matrix : abstract_matrix, abstract_block_matrix {
 21
 22};

块矩阵需要为两个运算符提供实现,以便它是矩阵或块矩阵,具体取决于上下文。我不知道如何提供特定于 block_matrix 类的实现。 现在,它是通过传递对象包装类型作为最后一个参数来完成的,但这看起来不太干净。我想保留纯矩阵表示法。

Is it possible to inherit identically named operator which only differ in return type, from two different abstract classes.
If so, them:

  • what is the syntax for implementing operators

  • what is the syntax for using/resolving operators

  • what is the overhead in general case, same as for any other virtual function?

if you can provide me with a reference or sample code that would be helpful

thanks

12struct abstract_matrix {
 13    virtual double& operator()(int i, int j);
 14};
 15
 16    struct abstract_block_matrix {
 17        virtual double* operator()(int i, int j);
 18    };
 19
 20struct block_matrix : abstract_matrix, abstract_block_matrix {
 21
 22};

block matrix needs to provide implementations for both operators, so that it is either a matrix or a block matrix, depending on the context. I do not know how to provide implementation specific to block_matrix class.
right now, it is done by passing object wrapped type as the last argument, but that does not seem very clean. I would like to retain pure matrix notation.

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

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

发布评论

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

评论(3

云归处 2024-08-24 02:10:24

函数的返回类型不是其签名的一部分,因此 block_matrix 中不能有两个运算符+(i,j) - 这将是一个不明确的调用。因此,多重继承在这一点上有点转移注意力。你就是不能那样做。

你真正想做什么,为什么?

无论如何,对于您的另一个问题:虚拟运算符在性能和操作方式方面与虚拟函数完全相同。使用它们的方式只是存在轻微的语义差异 - 但在幕后它们只是像其他函数一样的函数。

The return type of a function is not part of it's signature, so you can't have two operator+(i,j)'s in block_matrix - that would be an ambiguous call. So multiple inheritance is sort of a red herring here on this point. You just can't do that.

What are you really trying to do, and why?

In any event, for your other question: virtual operators are exactly like virtual functions in terms of performance and the way they operate. There are just slight semantic differences in how you use them - but under the hood they're just functions like any other.

只等公子 2024-08-24 02:10:24

您不能重载返回类型。当调用函数或运算符时,编译器必须知道要调用哪一个。它不会根据函数(运算符)调用分配的内容来推断。

看起来您正在寻求实现一些矩阵数学。也许如果您下载 DirectX SDK 或 OpenGL 并看看它们是如何实现的,您可能会得到一些关于如何正确实现的想法。

You can't overload on the return type. When a function or an operator is invoked the compiler has to know which one to call. It will not infer that based on what the function(operator) call assigned to.

Looks like your are looking to implement some matrix math. Perhaps if you download DirectX SDK or OpenGL and have a look how they do it, you might get some ideas on how to do it properly.

嗳卜坏 2024-08-24 02:10:24

我成功了,但它很奇怪。我确实喜欢模板。

template<class T>
class Base1
{
};

template<class T>
class Base2
{
};

class Derived;
template<>
class Base1<Derived>
{
public:
     double foo(){return 0.1;}
};

template<>
class Base2<Derived>
{
public:
    int foo(){return 1;}
};

class Derived
    : public Base1<Derived>
    , public Base2<Derived>
{
public:
    using Base1<Derived>::foo;
};

int main()
{
     double sum = 0;
     Derived d;
     sum += d.foo(); //+ .1
     Base1<Derived> * pI = &d;
     sum += pI->foo(); //+ .1

     Base2<Derived> * pF = &d;
     sum += pF->foo(); //+ 1

     return (sum*10);
}

如果没有模板,我就无法让它工作,尽管它看起来应该可以。我不确定您是否可以以同样的方式执行模板化成员函数,但我的直觉说“不”。

在代码组织方面,我将在 Derived 的定义或声明之后定义 Base# 内容,因为这确实是它的用途。请记住,您可以使用 typename Base1;一些让事情变得更漂亮的东西。

编辑:
哦,对了!它不允许您执行“使用”技巧或具有不同的返回类型,但它更简单:

class Derived
    : public Base1
    , public Base2
{
    double Base1::foo(){...}
    double Base2::foo(){...}
}

可能有一种可怕的、可怕的、很棒的方法来结合这两种方法,但我认为它实际上不会使用代码时提供帮助。我可能会就此回复你。

I got it work, but it's wonky. I do love templates.

template<class T>
class Base1
{
};

template<class T>
class Base2
{
};

class Derived;
template<>
class Base1<Derived>
{
public:
     double foo(){return 0.1;}
};

template<>
class Base2<Derived>
{
public:
    int foo(){return 1;}
};

class Derived
    : public Base1<Derived>
    , public Base2<Derived>
{
public:
    using Base1<Derived>::foo;
};

int main()
{
     double sum = 0;
     Derived d;
     sum += d.foo(); //+ .1
     Base1<Derived> * pI = &d;
     sum += pI->foo(); //+ .1

     Base2<Derived> * pF = &d;
     sum += pF->foo(); //+ 1

     return (sum*10);
}

I couldn't get it to work without templates, although it seems like it should be able to. I'm not sure if you can get away with just doing templated member functions in the same manner, but my gut says "no".

In terms of code organization, I would then define the Base# stuff right after the definition or declaration of Derived, since that's really what it's for. Keep in mind you can then use typename Base1<Derived> something to make things prettier.

Edit:
Oh, right! It doesn't allow you to do the "using" trick or have different return types, but it's otherwise simpler:

class Derived
    : public Base1
    , public Base2
{
    double Base1::foo(){...}
    double Base2::foo(){...}
}

There may be a terrible, horrible, awesome way to combine these two approaches, but I don't think it'll actually help out when using the code. I may get back to you on that.

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