Archived

This topic is now archived and is closed to further replies.

Question about templates?

This topic is 5148 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

If I define a template class like this: template< class T > class object1 { object1(); ~object1(); }; How do I create an instance of this class itself? I can create a derived class from this one and make an instance of that, but what happens if I just wanted to instance this class using itself if you know what I mean. For example, I can''t do this (which is what I''m want to do): object1 *test = new object1; In order for it to work I would have to do something like this for example: object1 *test = new object1; But what if I don''t want it to be an "int", what if a want it to be an "object1"! Like this: object1<object1> *test = new object1<object1>; Of course though that won''t work. But do you see what I''m trying to do? Any ideas about how to do this or is it not possible?

Share this post


Link to post
Share on other sites
Your basic syntax looks correct right there:
object1<object1> *test = new object1<object1>;


//You just do:

object1<unsigned long>* test = new object1<unsigned long>;

//Just replace unsigned long with whatever type you want.


//You can do the above (I believe) but you have to declare a type //for object1:


object1< object1<float> >* test = new object1< object1<float> >;

//Again, the float here is just an example.

//Hope that helps.




PS:
I should mention one thing before you run into it and drive yourself crazy trying to find the problem.


//This will generate a compile error:

object1<object1<float>> test;

//The closing '>>' gets interpreted as a bit shift operator.

//So include a space inbetween them:

object1<object1<float> > test;


Sorry I have to put everything in source tags or it gets formatted incorrectly.

[edited by - rayno on November 9, 2003 3:25:18 AM]

Share this post


Link to post
Share on other sites
I''m not exactly sure what you''re asking. Are you looking to instantiate an instance of a class from that template yourself instead of having the compiler do it for you?

If that''s what you''re looking to do this is how to do it:

Say you want an instance of a class using ints from your template. You add this at the end of your template source .cpp file:


template class object1<int>;
//now you can use this template in your code that uses

//int datatype


template class object1<char>;
//samething only this time it create the instance using char


template class object1<object1<int> >;
//after this declaration you can do

//object1<object1<int> > ObjFoo; in your source



If you do choose this way it''s a good idea to put these explicit instantiations in a different file and include that file at the end of your template source.





--{You fight like a dairy farmer!}

Share this post


Link to post
Share on other sites
I understand everything you have both said, except that''s not what I''m trying to do

I just want to use object1 like a regular class, just create an instance of "it" without making it an int or a float or whatever. However I want to keep it as a template so that I have the option of creating it as one of these data types later. To help understand this, just imagine that "object1" is a data type itself and I want to create an instance of the class using object1 as the data type (only because you are forced to specify a data type).

Share this post


Link to post
Share on other sites
GreatWolf - err. No.

Sandy - you will have to make the code that use the class templated too. How exactly depends on the specific cases.

For example, if you know you are going to use your object class, but don't know T yet, you can do something like


template<class T>
void MyFunction( object1<T>& obj )
{
/* ... */
}

object1<int> foo;
MyFunction( foo ); // calls MyFunction<int>(foo);



The point is that, outside of templated functions and classes, all template parameters must be fully resolved at compile time . You cannot create an actual instance of a templated class without specifying all the template parameters.

Templates provide compile-time polymorphism.
Run-time polymorphism is done through inheritance and virtual functions.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]


[edited by - Fruny on November 9, 2003 4:04:00 AM]

Share this post


Link to post
Share on other sites
So in other words it is not possible to use a templated class like a regular class

If I want to create an instance of a templated class then I will *have* to satisfy all the parameters. So I would have to do the following then:

class object1a
{
object1();
~object1();
};

template< class T > class object1b
{
object1();
~object1();
};

The first one allowing me to create an instance of object1 while the second class will allow me to use object1 as a template.

Share this post


Link to post
Share on other sites
quote:
Original post by sandy
So in other words it is not possible to use a templated class like a regular class



The class template is not yet a class. It is a model for a class. It''s like a form (heck, it''s a template) in which you fill the holes. Once you have determined the arguments, you do have an ordinary class - which you can use.

quote:

If I want to create an instance of a templated class then I will *have* to satisfy all the parameters. So I would have to do the following then:

/* code */

The first one allowing me to create an instance of object1 while the second class will allow me to use object1 as a template.


Instanciating a class "using itself" makes little sense (unless you are doing template recursion). I think your design is flawed. Please do explain why, if you want to instanciate "object1<object1>", you made it a template in the first place ? You probably really have two separate concepts here (as your latest post suggest).



[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites
Sometimes object1 is created using other objects as the parameter. However, I have run into situations where I just want to use object1 on its own (i.e. the parameter would be object1). Have a look at the new definition below.

template< class T > class object1
{
public:
object1();
~object1();

private:
T *next;
};

From the above definition it is obvious that object1 is used to create some sort of list. Now this is all fine if I instance object1 as the following.

object1<object2> *test = new object1<object2>;

This can be used to create a list of object2 objects. But what happens if I want to create a list of object1 objects? But you can''t do the following:

object1<object1> *test = new object1<object1>;

Share this post


Link to post
Share on other sites