returning pure virtual object

Discussion in 'C++' started by Philipp Kraus, Dec 28, 2012.

  1. Hello,

    I have got two pure virtual class like

    class Base1 {
    public :
    virtual Base2 getReturn( void ) const = 0;
    }


    class Base2 {
    public :
    virtual void doSomething() = 0;
    }


    My implementated classes shows


    class myReturn : public Base2 { .... }

    class myObj : public Base1 {
    public :

    myObj() : m_data( myReturn() );

    Base2 getReturn() { return m_data }

    private :
    myReturn m_data
    }

    On g++ I get an error "invalid initialization of reference of type". I would
    like to return a reference or copy of the internal object and I need only, that
    it is a derivated object from my pure virtual class.

    How can I do this in a correct way?

    Thanks for the explanation

    Phil
     
    Philipp Kraus, Dec 28, 2012
    #1
    1. Advertising

  2. On 12/28/2012 3:08 PM, Philipp Kraus wrote:
    > I have got two pure virtual class like


    They are actually called "abstract classes". "Pure" are the member
    functions.

    >
    > class Base1 {
    > public :
    > virtual Base2 getReturn( void ) const = 0;


    'Base2' seems undefined here.

    > }

    ;

    and try to avoid putting "void" between parentheses. You're being
    inconsistent (you didn't do that in the 'Base2' in the declaration of
    the 'doSomething' member), and it's a C-ism.
    >
    >
    > class Base2 {
    > public :
    > virtual void doSomething() = 0;
    > }

    ;
    >
    >
    > My implementated classes shows
    >
    >
    > class myReturn : public Base2 { .... }


    What's in '...'? Does it actually implement 'doSomething'?

    >
    > class myObj : public Base1 {
    > public :
    >
    > myObj() : m_data( myReturn() );


    Actually, "m_data()" should suffice.

    >
    > Base2 getReturn() { return m_data }


    This 'getReturn' does not override the 'Base1::getReturn' -- they have
    different types. Perhaps you meant

    Base2 getReturn() const { return m_data; }

    However, that won't do either. It *slices* the return value and
    converts (or, rather, attempts to convert) the object of the derived
    type ('myReturn') to the base class, which is *abstract*. IOW, it tries
    to *instantiate* an object of an abstract class.

    >
    > private :
    > myReturn m_data


    ;

    > }

    ;

    What's wrong with your keyboard? You seem to be missing a whole lot of
    semicolons...

    >
    > On g++ I get an error "invalid initialization of reference of type". I
    > would
    > like to return a reference or copy of the internal object and I need
    > only, that
    > it is a derivated object from my pure virtual class.


    You can't return a copy. In order to return a copy the return value of
    the function has to be of the derived type. You *can* return a
    reference to the base class, but you will need to change the declaration
    of your 'getReturn' member to something like

    const Base2& getReturn() const;

    > How can I do this in a correct way?


    "Correct" depends on the requirements. What are you trying to accomplish?

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Dec 28, 2012
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. IK
    Replies:
    2
    Views:
    622
    hemraj
    Jul 23, 2004
  2. Todd Aspeotis
    Replies:
    3
    Views:
    482
    Kanenas
    May 30, 2005
  3. John Goche
    Replies:
    10
    Views:
    774
    Marcus Kwok
    Dec 8, 2006
  4. Replies:
    7
    Views:
    599
    James Kanze
    May 2, 2007
  5. a
    Replies:
    7
    Views:
    372
    dasjotre
    Jun 28, 2007
Loading...

Share This Page