Topic: Abstract Member Function and Multiple Inheritance


Author: "Sheela" <sheela.takesh@gmx.net>
Date: Fri, 16 Dec 2005 09:54:05 CST
Raw View
Hi

I wondered what are the reasons for the following simple problem:

A class Inherits from two classes with the same virtual function. Ok,
this is ambiguous. But if one of the base classes has only as an
abstract member function, the compiler still grumbles. Why can the
compiler not deduct the non-abstract function? The compile "could"
"know" that the abstract member function is not the one i want.

shure a solution would be to define the function in the concrete sub
class and forward it to the right implementation in the parent. but no
typing should be allowed in the sub class ;)

plattofrm linux 2.6 g++ 3.4.4

#include <iostream>

using namespace std;

class Interface
{
public:
  virtual void func() = 0;
};

class Impl
{
public:
  virtual void func() { cout << "Impl::func()" << endl; };
};

class Concrete : public Impl, public Interface
{

};

int
main(int argc, char* argv[])
{
  Concrete c;
  c.func();
  return 0;
};

$ g++ -Wall -O2 -o virfunc virfunc.cpp
virfunc.cpp: In function `int main(int, char**)':
virfunc.cpp:25: error: cannot declare variable `c' to be of type
`Concrete'
virfunc.cpp:25: error:   because the following virtual functions are
abstract:
virfunc.cpp:8: error:  virtual void Interface::func()
virfunc.cpp:26: error: request for member `func' is ambiguous
virfunc.cpp:8: error: candidates are: virtual void Interface::func()
virfunc.cpp:14: error:                 virtual void Impl::func()

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "Greg Herlihy" <greghe@pacbell.net>
Date: Fri, 16 Dec 2005 22:17:54 CST
Raw View
Sheela wrote:
> Hi
>
> I wondered what are the reasons for the following simple problem:
>
> A class Inherits from two classes with the same virtual function. Ok,
> this is ambiguous. But if one of the base classes has only as an
> abstract member function, the compiler still grumbles. Why can the
> compiler not deduct the non-abstract function? The compile "could"
> "know" that the abstract member function is not the one i want.

How is the compiler to "know" that you intend to call the non-abstract
method? And what if you changed your mind and decided to call the
abstract method instead? How would the compiler "know" to make the
adjustment when the source code itself would not have changed?

It's true that an abstract method is not always defined in the abstract
class. So perhaps the suggestion is to resolve the ambiguous function
call in the case that only one method is defined.

There are two problems with this approach. First, the compiler cannot
readily determine whether a particular method is defined in a program.
A linker can make that determination once all the source files have
been compiled, but that is far too late to be helpful here. Second, are
the side effects; deleting or adding a method in one source file could
suddenly cause some other source file to no longer compile. Generally
those types of surprises are best avoided.

Greg

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]