Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old July 19th, 2005, 06:26 PM
Tristan
Guest
 
Posts: n/a
Default Returning instance of necessary derived class

I have a function:

AbstractClass* getObject()
{
AbstractClass* d = new DerivedClass;

return d;
}

What I want is for this function to return an
actual instance of the derived class not a
pointer to it. This can't be done because the
function can't be declared as

AbstractClass getObject()

Inside this function I will be declaring the derived
type based on a code read from a file which is
why the correct object must be created and returned.

If there is no workaround how could the memory
declared by new DerivedClass be freed after
getObject has been used in the calling code?

Many Thanks

Tristan.
  #2  
Old July 19th, 2005, 06:26 PM
Gianni Mariani
Guest
 
Posts: n/a
Default Re: Returning instance of necessary derived class

Tristan wrote:
....[color=blue]
>
> If there is no workaround how could the memory
> declared by new DerivedClass be freed after
> getObject has been used in the calling code?[/color]

Obviously you need to "delete" the derived class. This may be done like:

1. Using a virtual destructor:

e.g.
class A { public: virtual ~A() {} };

class D : public A { };

A * a = new D;

delete a; // actually calls ~D;

2. A bad nasty nasty yukky way.

A * a = new D;

D * d = static_cast<D*>(a);

delete d;

3. You can use a "method" to delete (also yukky)
(can be combined with reference counting to be not yukky)

class A {
protected: ~A() {}
public: virtual RemoveMe() = 0;
};

class D : public A {
virtual RemoveMe() { delete this; }
};

A * a = new D;
a->RemoveMe();

.... probably a few more.


  #3  
Old July 19th, 2005, 06:28 PM
Christian Brechbühler
Guest
 
Posts: n/a
Default Re: Returning instance of necessary derived class

Tristan wrote:[color=blue]
> I have a function:
>
> AbstractClass* getObject()
> {
> AbstractClass* d = new DerivedClass;
>
> return d;
> }
>
> What I want is for this function to return an
> actual instance of the derived class not a
> pointer to it.[/color]

Why do you want that? Can you post a sample of ideal code using your class?

The reason I ask is that if you return an object (rather than a pointer
or reference) into code that doesn't know the exact type, you get
slicing (see 12.2.3). Which is almost certainly not what you want.
[color=blue]
> Inside this function I will be declaring the derived
> type based on a code read from a file which is
> why the correct object must be created and returned.[/color]

I assume you mean that getObject does something like this,

AbstractClass* getObject()
{
int code;
in_stream >> code;

if (code = 42)
return new DerivedClassA();
else
return new DerivedClassB();
}

You seem interested in runtime polymorphism, which you only get if you
manipulate objects through pointers and references (see 12.2.6).

Christian

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,338 network members.