Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Create object instance of different types

Reply
Thread Tools

Create object instance of different types

 
 
f.f
Guest
Posts: n/a
 
      01-03-2007
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

 
Reply With Quote
 
 
 
 
Andre Kostur
Guest
Posts: n/a
 
      01-03-2007
"f.f" <> wrote 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"?
 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      01-03-2007
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"
 
Reply With Quote
 
Sandip Shahane
Guest
Posts: n/a
 
      01-03-2007
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"


 
Reply With Quote
 
f.f
Guest
Posts: n/a
 
      01-04-2007
Thank you all. I will try the factory pattern.

On Jan 4, 12:20 am, red floyd <no.s...@here.dude> wrote:
> 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"


 
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
How do I create an instance of a composite templated upon a different type? mat C++ 0 12-22-2009 10:27 PM
Different behaviour in different struct declaration types __PaTeR C Programming 7 01-01-2009 12:13 AM
Object instance "reporting" to a container class instance Daniel Lipovetsky Python 2 03-12-2007 10:35 AM
Can you create an instance of a subclass with an existing instance of the base class? Sandra-24 Python 18 04-29-2006 04:01 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57