Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > returning pure virtual object

Reply
Thread Tools

returning pure virtual object

 
 
Philipp Kraus
Guest
Posts: n/a
 
      12-28-2012
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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      12-28-2012
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
 
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
Need clarification for virtual method and pure virtual function a C++ 7 06-28-2007 12:38 PM
virtual vs pure virtual member function sam_cit@yahoo.co.in C++ 7 05-02-2007 10:00 AM
private virtual functions and pure virtual functions with bodies John Goche C++ 10 12-08-2006 04:00 PM
Pure functions still pure after definition Todd Aspeotis C++ 3 05-30-2005 03:53 AM
virtual function and pure virtual function in the implementation of COM IK C++ 2 07-23-2004 02:55 PM



Advertisments