Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ multiple inheritance off identically named operator

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.

like image 814
Anycorn Avatar asked Mar 08 '26 00:03

Anycorn


1 Answers

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.

like image 56
Terry Mahaffey Avatar answered Mar 09 '26 14:03

Terry Mahaffey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!