473,366 Members | 1,161 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,366 software developers and data experts.

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.
Jul 19 '05 #1
2 1755
Tristan wrote:
....

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


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.
Jul 19 '05 #2
Tristan wrote:
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.
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.
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.


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

Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Sridhar R | last post by:
Consider the code below, class Base(object): pass class Derived(object): def __new__(cls, *args, **kwds): # some_factory returns an instance of Base # and I have to derive from this...
3
by: J.J. Feminella | last post by:
(Please disregard the previous message; I accidentally sent it before it was completed.) I have source code similar to the following. public class Vehicle { protected string dataV; // ......
3
by: Jordan Taylor | last post by:
Hi, can anyone suggest how the compiler will decide which bar() to invoke when we call d.dio()? I am getting the following output: B foo B bar but is there a situation when the same code can...
4
by: santel | last post by:
Hi all, I have these classes public class Base { private string str; private string str1; public string MyStr; {
5
by: D Witherspoon | last post by:
What is happening is that I have a class (ClassA) that inherits a class (ClassB) which inherits System.Net.Mail.MailMessage Project 1 references Project 2, Project 2 references Project 3. ...
15
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I met with a strange issue that derived class function can not access base class's protected member. Do you know why? Here is the error message and code. error C2248:...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.