Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Why doesn't this multiple virtual inheritance code compile?

Reply
Thread Tools

Why doesn't this multiple virtual inheritance code compile?

 
 
Chris Stankevitz
Guest
Posts: n/a
 
      01-02-2012

My intention is to
- Create an abstract base class "Shape" that must be an "Observer"
- Create an class "Square" that is a "Shape" and also an
"ObserverImp"

I thought I could do this like so:

struct Observer
{
virtual void Notify() = 0;
};

struct ObserverImp : public Observer
{
void Notify() {}
};

struct Shape : public virtual Observer
{
};

struct Square : public Shape, public ObserverImp
{
};

Shape* ShapeFactory()
{
return new Square;
}

$ g++ -Wall -c test.cpp
test.cpp: In function 'Shape* ShapeFactory()':
test.cpp:21:14: error: cannot allocate an object of abstract type
'Square'
test.cpp:16:1: note: because the following virtual functions are
pure within 'Square':
test.cpp:3:16: note: virtual void Observer::Notify()
test.cpp:22:1: warning: control reaches end of non-void function

Q: How can I do this using c++?

Thank you,

Chris
 
Reply With Quote
 
 
 
 
Chris Stankevitz
Guest
Posts: n/a
 
      01-02-2012
On Jan 2, 1:36*pm, Paavo Helde <(E-Mail Removed)> wrote:
> You could try to make this 'public virtual Observer'.


Thank you, yes I overlooked this. Although this does not help the
compile problem.

> That said, I'm not at all convinced that virtual inheritance is the right
> approach here.


Me too -- if it doesn't compile, it probably is not the correct
approach!

How would you go about accomplishing this with C++, if it is possible:
- Create an abstract base class "Shape" that must be an "Observer"
- Create an class "Square" that is a "Shape" and uses "ObserverImp"
to implement the "Observer" behavior.

Thank you,

Chris
 
Reply With Quote
 
 
 
 
Chris Stankevitz
Guest
Posts: n/a
 
      01-02-2012
On Jan 2, 2:23*pm, Robert Wessel <(E-Mail Removed)> wrote:
> Instantiating Square requires instantiating both an Observer and
> ObserverImp under the hood.


Yes, apparently so. I naively thought using "virtual inheritance"
would make it so that both base classes need not be instantiated.

> The obvious solution is to add an
> implementation of the required function to Square:


I am trying to avoid that particular obvious solution as it involves
me typing the same code in all of my Shape implementations. Plus I've
already written that code: it's in ObserverImp.

Thank you,

Chris
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      01-02-2012
On 01/ 3/12 11:41 AM, Chris Stankevitz wrote:
> On Jan 2, 1:36 pm, Paavo Helde<(E-Mail Removed)> wrote:
>> You could try to make this 'public virtual Observer'.

>
> Thank you, yes I overlooked this. Although this does not help the
> compile problem.
>
>> That said, I'm not at all convinced that virtual inheritance is the right
>> approach here.

>
> Me too -- if it doesn't compile, it probably is not the correct
> approach!
>
> How would you go about accomplishing this with C++, if it is possible:
> - Create an abstract base class "Shape" that must be an "Observer"
> - Create an class "Square" that is a "Shape" and uses "ObserverImp"
> to implement the "Observer" behavior.


If you don't actually require dynamic binding, you could make Observer a
template with Imp as it's parameter:

template <typename Imp> struct Observer
{
void Notify() { Imp::notify(); }
};

struct Imp
{
void Notify() {}
};

struct Shape : Observer<Imp>
{
};

struct Square : Shape
{
};

--
Ian Collins
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      01-02-2012
On 02.01.2012 21:16, Chris Stankevitz wrote:
> My intention is to
> - Create an abstract base class "Shape" that must be an "Observer"
> - Create an class "Square" that is a "Shape" and also an
> "ObserverImp"
>
> I thought I could do this like so:
>
> struct Observer
> {
> virtual void Notify() = 0;
> };
>
> struct ObserverImp : public Observer
> {
> void Notify() {}
> };


Use virtual inheritance for the interface (that is for `Observer`).


> struct Shape : public virtual Observer
> {
> };
>
> struct Square : public Shape, public ObserverImp
> {
> };


Technically OK.


> Shape* ShapeFactory()


Please use different naming conventions for types and functions.


> {
> return new Square;
> }


No. Forget the Java-isms. You don't need any
factory-manager-singleton-envelope-blahblah complication.

Or at least, if you absolutely have to code Java in C++, then do it
properly.

E.g., then return a smart pointer, not a raw pointer.


> $ g++ -Wall -c test.cpp
> test.cpp: In function 'Shape* ShapeFactory()':
> test.cpp:21:14: error: cannot allocate an object of abstract type
> 'Square'
> test.cpp:16:1: note: because the following virtual functions are
> pure within 'Square':
> test.cpp:3:16: note: virtual void Observer::Notify()
> test.cpp:22:1: warning: control reaches end of non-void function
>
> Q: How can I do this using c++?


See above.

It then compiles nicely, and corresponds directly to Java-like
implementation inheritance for an interface.

If you run into any problems then please post to a new thread with your
exact code and compiler invocation.


Cheers & hth.,

- Alf
 
Reply With Quote
 
Chris Stankevitz
Guest
Posts: n/a
 
      01-03-2012
On Jan 2, 3:56*pm, "Paul" <pchrist<nospam>(E-Mail Removed)> wrote:
> class Shapeublic observerImp{};


Paul,

I do not want all Shapes to be observerImps. I want all shapes to be
observers. I suspect what I want is not possible with c++.

Thank you,

Chris
 
Reply With Quote
 
Chris Stankevitz
Guest
Posts: n/a
 
      01-03-2012
On Jan 2, 3:46*pm, "Alf P. Steinbach" <alf.p.steinbach
(E-Mail Removed)> wrote:
> If you run into any problems then please post to a new thread with your
> exact code and compiler invocation.


Alf,

I attempted what you suggested (minus the style, factory, auto_ptr,
and new thread suggestions). What I came up with did not compile.
(Compiler invocation appears below). Would you please tell me how I
can modify this code so that it compiles and implements:
- Create an abstract base class "Shape" that must be an "Observer"
- Create a class "Square" that is a "Shape" and uses "ObserverImp" to
implement the "Observer" behavior.

Thank you,

Chris

//-----

struct Observer
{
virtual void Notify() = 0;
};

struct ObserverImp : public virtual Observer
{
void Notify() {}
};

struct Shape : public Observer
{
};

struct Square : public Shape, public ObserverImp
{
};

Shape* ShapeFactory()
{
return new Square;
}

//-----

$ g++ -Wall -c test.cpp
test.cpp: In function 'Shape* ShapeFactory()':
test.cpp:21:14: error: cannot allocate an object of abstract type
'Square'
test.cpp:16:1: note: because the following virtual functions are
pure within 'Square':
test.cpp:3:16: note: virtual void Observer::Notify()
test.cpp:22:1: warning: control reaches end of non-void function
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      01-03-2012
Chris Stankevitz <(E-Mail Removed)> wrote:
> test.cpp: In function 'Shape* ShapeFactory()':
> test.cpp:21:14: error: cannot allocate an object of abstract type
> 'Square'
> test.cpp:16:1: note: because the following virtual functions are
> pure within 'Square':
> test.cpp:3:16: note: virtual void Observer::Notify()


How about you try to read what the compiler is telling you? It's telling
to the exact reason why you cannot instantiate the class: You have a pure
virtual function in the base class that is not implemented in the derived
class.
 
Reply With Quote
 
Goran
Guest
Posts: n/a
 
      01-03-2012
On Jan 3, 8:20*am, Paavo Helde <(E-Mail Removed)> wrote:
> Chris Stankevitz <(E-Mail Removed)> wrote innews:(E-Mail Removed):
>
> > On Jan 2, 1:36*pm, Paavo Helde <(E-Mail Removed)> wrote:
> >> You could try to make this 'public virtual Observer'.

>
> > Thank you, yes I overlooked this. *Although this does not help the
> > compile problem.

>
> Strange, compiles fine with my MSVC2010 and gcc (though MSVC issues a
> strange warning):


AFAICanSee, what Paavo suggested is __the__ solution for your
conundrum. Comeau also (obviously) compiles this. Chris, what is the
compiler error?

Goran.
 
Reply With Quote
 
Spike
Guest
Posts: n/a
 
      01-03-2012
On 03/01/2012 05:38, Chris Stankevitz wrote:
> struct Shape : public Observer
> {
> };


struct Shape : public virtual Observer
{
};

Your code was missing one virtual modifier.

Also you need to declare the base classes destructors as virtual.
See http://en.wikipedia.org/wiki/Virtual...al_destructors
for an explanation.

HTH

--
http://www.econet-project.eu
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Virtual inheritace -- when one inheritance of the base is virtual andthe other isn't. pauldepstein@att.net C++ 1 03-14-2009 03:45 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
virtual inheritance and virtual function. Ashwin C++ 2 08-01-2006 12:48 PM
mul. inheritance & overloading operator new/delete solved by virtual base inheritance? cppsks C++ 0 10-27-2004 07:49 PM
Should 'public virtual' always become 'private virtual'? & using private inheritance qazmlp C++ 19 02-04-2004 12:37 AM



Advertisments