Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #13011

Re: Why doesn't this multiple virtual inheritance code compile?

From "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>
Newsgroups comp.lang.c++
Subject Re: Why doesn't this multiple virtual inheritance code compile?
Date 2012-01-03 00:46 +0100
Organization A noiseless patient Spider
Message-ID <jdtfkp$rcb$1@dont-email.me> (permalink)
References <785a07d6-7cf7-4249-a123-f7d7b4aaa8b3@l24g2000yqm.googlegroups.com>

Show all headers | View raw


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

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Why doesn't this multiple virtual inheritance code compile? Chris Stankevitz <chrisstankevitz@gmail.com> - 2012-01-02 12:16 -0800
  Re: Why doesn't this multiple virtual inheritance code compile? Paavo Helde <myfirstname@osa.pri.ee> - 2012-01-02 15:36 -0600
    Re: Why doesn't this multiple virtual inheritance code compile? Chris Stankevitz <chrisstankevitz@gmail.com> - 2012-01-02 14:41 -0800
      Re: Why doesn't this multiple virtual inheritance code compile? Ian Collins <ian-news@hotmail.com> - 2012-01-03 11:54 +1300
      Re: Why doesn't this multiple virtual inheritance code compile? "Paul" <pchrist<nospam>or@yahoo.co.uk> - 2012-01-02 23:56 +0000
        Re: Why doesn't this multiple virtual inheritance code compile? Chris Stankevitz <chrisstankevitz@gmail.com> - 2012-01-02 20:25 -0800
          Re: Why doesn't this multiple virtual inheritance code compile? Marcel Müller <news.5.maazl@spamgourmet.com> - 2012-01-03 12:46 +0100
          Re: Why doesn't this multiple virtual inheritance code compile? "Paul" <pchrist<nospam>or@yahoo.co.uk> - 2012-01-03 16:34 +0000
      Re: Why doesn't this multiple virtual inheritance code compile? Paavo Helde <myfirstname@osa.pri.ee> - 2012-01-03 01:20 -0600
        Re: Why doesn't this multiple virtual inheritance code compile? Goran <goran.pusic@gmail.com> - 2012-01-02 23:52 -0800
        Re: Why doesn't this multiple virtual inheritance code compile? "Paul" <pchrist<nospam>or@yahoo.co.uk> - 2012-01-03 15:42 +0000
        Re: Why doesn't this multiple virtual inheritance code compile? Chris Stankevitz <chrisstankevitz@gmail.com> - 2012-01-03 13:35 -0800
  Re: Why doesn't this multiple virtual inheritance code compile? Robert Wessel <robertwessel2@yahoo.com> - 2012-01-02 16:23 -0600
    Re: Why doesn't this multiple virtual inheritance code compile? Chris Stankevitz <chrisstankevitz@gmail.com> - 2012-01-02 14:43 -0800
  Re: Why doesn't this multiple virtual inheritance code compile? "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2012-01-03 00:46 +0100
    Re: Why doesn't this multiple virtual inheritance code compile? Chris Stankevitz <chrisstankevitz@gmail.com> - 2012-01-02 20:38 -0800
      Re: Why doesn't this multiple virtual inheritance code compile? Spike <invalid@invalid.invalid> - 2012-01-03 09:35 +0100
        Re: Why doesn't this multiple virtual inheritance code compile? Chris Stankevitz <chrisstankevitz@gmail.com> - 2012-01-03 13:39 -0800
    Re: Why doesn't this multiple virtual inheritance code compile? Leigh Johnston <leigh@i42.co.uk> - 2012-01-03 21:51 +0000
      Re: Why doesn't this multiple virtual inheritance code compile? "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2012-01-04 02:14 +0100
        Re: Why doesn't this multiple virtual inheritance code compile? Leigh Johnston <leigh@i42.co.uk> - 2012-01-04 01:19 +0000
          Re: Why doesn't this multiple virtual inheritance code compile? Ian Collins <ian-news@hotmail.com> - 2012-01-04 15:15 +1300
            Re: Why doesn't this multiple virtual inheritance code compile? Leigh Johnston <leigh@i42.co.uk> - 2012-01-04 02:58 +0000
        Re: Why doesn't this multiple virtual inheritance code compile? jgk@panix.com (Joe keane) - 2012-01-04 20:45 +0000
  Re: Why doesn't this multiple virtual inheritance code compile? Juha Nieminen <nospam@thanks.invalid> - 2012-01-03 06:26 +0000
  Re: Why doesn't this multiple virtual inheritance code compile? jgk@panix.com (Joe keane) - 2012-01-03 14:12 +0000

csiph-web