473,386 Members | 2,078 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,386 software developers and data experts.

Create object instance of different types

f.f
class A
{
....
};

class B : public A
{
....
};

class C : public A
{
....
};

T* createObject()
{
T* ptr = new T();
return ptr;
}

I wanna use createObject() to create object instance of either class B
or class C?
What approach should I use? Should I use template?

f.f

Jan 3 '07 #1
4 1362
"f.f" <ah******@gmail.comwrote in news:1167836437.480073.131750
@i12g2000cwa.googlegroups.com:
class A
{
...
};

class B : public A
{
...
};

class C : public A
{
...
};

T* createObject()
{
T* ptr = new T();
return ptr;
}

I wanna use createObject() to create object instance of either class B
or class C?
What approach should I use? Should I use template?
How does createObject know which of A, B, or C to create? Offhand I
don't see how a template makes it easier:

template<class T>
T * createObject()
{
return new T();
}

All this seems to do is obfuscate the code by hiding the use of "new",
since you'd have to specify the type to be created at the call site
anyway. Why not just use "new" whererever you're using "createObject"?
Jan 3 '07 #2
f.f wrote:
class A
{
...
};

class B : public A
{
...
};

class C : public A
{
...
};

T* createObject()
{
T* ptr = new T();
return ptr;
}

I wanna use createObject() to create object instance of either class B
or class C?
What approach should I use? Should I use template?
You might want to google for "Factory Pattern"
Jan 3 '07 #3
And if you have not already got the precise reference, foll. might just
be something you can do.
http://www.codeproject.com/cpp/SimpleDynCreate.asp

this is specially useful if u wanna add new derived classes in future.
also along with extensibility, some folks try to make the class and its
creator function registeration externally or thru macro as in

http://www.codeproject.com/cpp/ratkfactory.asp

sandip shahane
>
You might want to google for "Factory Pattern"
Jan 3 '07 #4
f.f
Thank you all. I will try the factory pattern.

On Jan 4, 12:20 am, red floyd <no.s...@here.dudewrote:
f.fwrote:
class A
{
...
};
class B : public A
{
...
};
class C : public A
{
...
};
T* createObject()
{
T* ptr = new T();
return ptr;
}
I wanna use createObject() to create object instance of either class B
or class C?
What approach should I use? Should I use template?You might want to google for "Factory Pattern"
Jan 4 '07 #5

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

Similar topics

18
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of...
8
by: Steve Neill | last post by:
Can anyone suggest how to create an arbitrary object at runtime WITHOUT using the deprecated eval() function. The eval() method works ok (see below), but is not ideal. function Client() { }...
16
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
2
by: Angel Of Death | last post by:
I have a method. It takes some XML as a parameter. Depending on the content of the XML it should create a specific object and call a KNOWN method. So: public void PersistXml(string XmlData){} ...
5
by: JH | last post by:
Hi I found that a type/class are both a subclass and a instance of base type "object". It conflicts to my understanding that: 1.) a type/class object is created from class statement 2.) a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.