Complex C ++ inheritance from an operator with the same name

Is it possible to inherit an identically named operator, which differs only from the return type, from two different abstract classes. If yes, then they:

  • what is the syntax for implementing operators

  • what is the syntax for using / resolving operators

  • What is the overhead in the general case, as with any other virtual function?

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

thank

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

the block matrix must provide implementations for both operators, so this is either a matrix or a block matrix, depending on the context. I do not know how to provide an implementation specific to the block_matrix class. right now, this is being done by passing the wrapped type object as the last argument, but it doesn't seem very clean. I would like to keep a clean matrix notation.

+3
c ++ inheritance multiple-inheritance virtual
Jan 01 '09 at 23:02
source share
3 answers

The return type of a function is not part of its signature, so you cannot have two + (i, j) operators in block_matrix - this will be an ambiguous call. Thus, multiple inheritance here looks like a red herring. You just can't do it.

What are you really trying to do and why?

Anyway, for your other question: virtual operators are exactly like virtual functions in terms of performance and how they work. There are only slight semantic differences in how you use them, but under the hood they just function like any other.

+1
Jan 01 '09 at 23:32
source share

You cannot overload the return type. When a function or statement is called, the compiler must know which one to call. He will not conclude that based on what function (operator) call is assigned.

It looks like you want to implement math math. Perhaps if you download the DirectX SDK or OpenGL and see how they do it, you might get some ideas on how to do it right.

+1
Jan 01 '09 at 23:32
source share

I did it, but it's terrible. I really 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 could not get it to work without templates, although it seems that it should be able to. I'm not sure you can just perform the template member functions in the same way, but my gut says no.

In terms of organizing the code, I would then define Base # material immediately after defining or declaring Derived, as that is really what it is for. Keep in mind that you can use typename Base1<Derived> something to make things typename Base1<Derived> something prettier.

Edit: Oh right! This does not allow you to use the β€œuse” trick or have different return types, but otherwise easier:

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

It may be a terrible, terrible, amazing way to combine these two approaches, but I don’t think it really helps when using the code. I can come back to you.

0
Jan 02 '09 at 4:34
source share



All Articles