0

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..:'(

5
Contributors
12
Replies
16
Views
9 Years
Discussion Span
Last Post by rhoit
0

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:(

0

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...

0

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

0

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.

0

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..

0

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"?

0

I believe he's trying to do something like:

typedef box MyGreatBeautifulBox;

//and use it as:
MyGreatBeutifulBox<int> a;
MyGreatBeutifulBox<char>b;

I don't think it can be done

0

Hehe...

template<typename T> 
struct MyGreatBeautifulBox:box<T>{};

int main()
{
    MyGreatBeautifulBox<int>  ibox;
    MyGreatBeautifulBox<char> cbox;
1

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

Votes + Comments
Funny guy...:D
0

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...

0

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:

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.