How to use typedef for the template structure? What its syntax?
I create the simple template structure like this
#include <iostream>
template <class info>
struct box
{
info data;
};
int main()
{
box<int> b1;
b1.data = 1;
std::cout<<b1.data;
return 0;typedef box square;
}
Now i want to use the typedef for this template structure.
I could not figure out the syntax...
as i tried
typedef box square;
But i didn't workout... Please help me..:'(
Rhohitman
Junior Poster in Training
83 posts since Dec 2007
Reputation Points: 10
Solved Threads: 5
I don't think it's in the language.
You could use #define square box.
You may want to google template typedefs, or check out http://www.gotw.ca/gotw/079.htm for some solutions.
Couldn't understand your like... but might come handy later... thanx:(
Rhohitman
Junior Poster in Training
83 posts since Dec 2007
Reputation Points: 10
Solved Threads: 5
What's a problem?
typedef box<int> MyFavouriteBoxType;
...
MyFavouriteBoxType b1;
Look at STL headers on you C++ installation. There are lots of (useful) typedefs in STL templates...
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
yea, you gotta either macro it or typedef with specifying the actual template type :P sucks in a way... but hey, what's the deal with the box thing, it's pretty small and easy to write...
i use typedef for long, looooong names, like:
typedef my_monumental_structure<another_huge_type>::iterator sit;
and then i just sit... all the way... rocks
gregorynoob
Junior Poster in Training
86 posts since Jun 2008
Reputation Points: 12
Solved Threads: 5
What's a problem?
typedef box<int> MyFavouriteBoxType;
...
MyFavouriteBoxType b1;
Look at STL headers on you C++ installation. There are lots of (useful) typedefs in STL templates...
Is there any possibilities to have generic type typedef. As normally typedef copies complete characteristics of it host.
Rhohitman
Junior Poster in Training
83 posts since Dec 2007
Reputation Points: 10
Solved Threads: 5
yea, you gotta either macro it or typedef with specifying the actual template type :P sucks in a way... but hey, what's the deal with the box thing, it's pretty small and easy to write...
i use typedef for long, looooong names, like:
typedef my_monumental_structure<another_huge_type>::iterator sit;
and then i just sit... all the way... rocks
I didn't get what your syntax says. :confused:
Can you kindly explain it..
Rhohitman
Junior Poster in Training
83 posts since Dec 2007
Reputation Points: 10
Solved Threads: 5
Typedef name is an alias for the type, it's not a new type name.
Can you explain what a problem are you trying to solve with "generic typedef"?
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
Hehe...
template<typename T>
struct MyGreatBeautifulBox:box<T>{};
int main()
{
MyGreatBeautifulBox<int> ibox;
MyGreatBeautifulBox<char> cbox;
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
I didn't get what your syntax says. :confused:
Can you kindly explain it..
umm, sorry if it was not understandable, i tried to come up with a very long name for a type i want to define... so i made the name myself, don't try it it won't work. unless you define such a class. which would be silly... c++ isn't java :P
gregorynoob
Junior Poster in Training
86 posts since Jun 2008
Reputation Points: 12
Solved Threads: 5
Typedef name is an alias for the type, it's not a new type name.
Can you explain what a problem are you trying to solve with "generic typedef"?
I got your point..
But lets say If i want to create the pointer variable.
like for example normally what we do is
struct box
{
... blah .. blah.. blah
};
typedef box *crazy_box;
//as we can now easy create the pointer with crazy_box
crazy_box B1;
Now I want this to happen with the template structure what to do...
Rhohitman
Junior Poster in Training
83 posts since Dec 2007
Reputation Points: 10
Solved Threads: 5
Hehe...
template<typename T>
struct MyGreatBeautifulBox:box<T>{};
int main()
{
MyGreatBeautifulBox<int> ibox;
MyGreatBeautifulBox<char> cbox;
Hey that Cheating. This is not what i meant for.. :sad:
Rhohitman
Junior Poster in Training
83 posts since Dec 2007
Reputation Points: 10
Solved Threads: 5