Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,605 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 930 people online right now. Registration is fast and FREE... Join Now!




making an object from an unknown type?

 
Reply to this topicStart new topic

making an object from an unknown type?

The Architect 2.0
post 15 Jul, 2008 - 08:54 PM
Post #1


D.I.C Head

**
Joined: 22 May, 2008
Posts: 56

how would you go about making an object of an unspecified type?

basically, i want to 'take' the type of a STL container and make an object that is of the same type as its elements, WITHOUT knowing what the type is.

I need to add another element into the container, without knowing the actual type of the object.

ex.

if i was given a list<Dog> conainer, the function would create a Dog object. if i was given a list<String> container, i would be given a String. all in a generic manner


also, provided the above is possible, how would i initiate its default constructor(provided it's not 'covered' in the declaration).
User is offlineProfile CardPM

Go to the top of the page

captainhampton
post 16 Jul, 2008 - 05:07 AM
Post #2


Jawsome++;

Group Icon
Joined: 17 Oct, 2007
Posts: 518



Thanked 2 times

Dream Kudos: 825
My Contributions


I think you may be hinting towards the use of templates:
http://www.cplusplus.com/doc/tutorial/templates.html



User is offlineProfile CardPM

Go to the top of the page

polymath
post 16 Jul, 2008 - 07:22 AM
Post #3


D.I.C Regular

Group Icon
Joined: 4 Apr, 2008
Posts: 407



Thanked 4 times

Dream Kudos: 500
My Contributions


Definatly look at that link.
User is offlineProfile CardPM

Go to the top of the page

The Architect 2.0
post 16 Jul, 2008 - 03:35 PM
Post #4


D.I.C Head

**
Joined: 22 May, 2008
Posts: 56

cpp

template<class T>
T& makeObject(list<T> &container)
{
T newobject = new T;
return newobject;
}


basically, what i want to do is that. i think its pretty obvious what its doing, but just incase; it is taking a container and making a new object on the free store that is the same type as the container's element type.

i THINK the way i written it should result in ONE object bring created that still exists after the function returns and that the reference being returned is still valid.
User is offlineProfile CardPM

Go to the top of the page

skaoth
post 16 Jul, 2008 - 05:51 PM
Post #5


D.I.C Regular

Group Icon
Joined: 7 Nov, 2007
Posts: 338



Thanked 9 times

Dream Kudos: 100
My Contributions


Almost.

You don't need to pass in a list of type T to create a new object of type T. The template declaration should take care of that. Here is a fixed version
CODE

template<class T>  
T* makeObject()  
{  
     T* newobject = new T();  
     return newobject;    
}
User is offlineProfile CardPM

Go to the top of the page

The Architect 2.0
post 16 Jul, 2008 - 07:01 PM
Post #6


D.I.C Head

**
Joined: 22 May, 2008
Posts: 56

um....i want to make an object from w/e type the list is using as its value-type
User is offlineProfile CardPM

Go to the top of the page

skaoth
post 16 Jul, 2008 - 07:18 PM
Post #7


D.I.C Regular

Group Icon
Joined: 7 Nov, 2007
Posts: 338



Thanked 9 times

Dream Kudos: 100
My Contributions


what does w/e mean?

*Edit

I think I understand what your looking for.
You are correct in saying that you need to pass in the list of type T if you want the compiler to implicitly determine the type.

CODE

template<class T>  
T* makeObject(list<T> list)  
{  
     T* newobject = new T();  
     return newobject;    
}

// ...  in main
void main(void)
{
        std::list<double> mylist;
    double *d = makeObject(mylist);
}


Then again I don't know the context (I would have to see more) from which you are making the call to makeObject().
If you are doing it like the example above then there is no reason to pass in the list to
the function as it could be called like makeObject<double>()

This post has been edited by skaoth: 16 Jul, 2008 - 08:10 PM
User is offlineProfile CardPM

Go to the top of the page

The Architect 2.0
post 16 Jul, 2008 - 08:40 PM
Post #8


D.I.C Head

**
Joined: 22 May, 2008
Posts: 56

thanks skaoth. the pointer solution works fine.

but what is the issue with using a reference?
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 02:16AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month