DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   How to use typedef for the template structure? What its syntax? (http://www.daniweb.com/forums/thread150300.html)

Rhohitman Oct 10th, 2008 2:32 am
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..:'(

dougy83 Oct 10th, 2008 4:20 am
Re: How to use typedef for the template structure? What its syntax?
 
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.

Rhohitman Oct 10th, 2008 8:50 am
Re: How to use typedef for the template structure? What its syntax?
 
Quote:

Originally Posted by dougy83 (Post 709263)
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:(

ArkM Oct 10th, 2008 10:28 am
Re: How to use typedef for the template structure? What its syntax?
 
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...

gregorynoob Oct 10th, 2008 11:22 am
Re: How to use typedef for the template structure? What its syntax?
 
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

Rhohitman Oct 10th, 2008 12:47 pm
Re: How to use typedef for the template structure? What its syntax?
 
Quote:

Originally Posted by ArkM (Post 709467)
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 Oct 10th, 2008 1:50 pm
Re: How to use typedef for the template structure? What its syntax?
 
Quote:

Originally Posted by gregorynoob (Post 709510)
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..

ArkM Oct 10th, 2008 3:30 pm
Re: How to use typedef for the template structure? What its syntax?
 
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"?

Sci@phy Oct 10th, 2008 3:58 pm
Re: How to use typedef for the template structure? What its syntax?
 
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

ArkM Oct 10th, 2008 5:02 pm
Re: How to use typedef for the template structure? What its syntax?
 
Hehe...
template<typename T> 
struct MyGreatBeautifulBox:box<T>{};

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


All times are GMT -4. The time now is 8:00 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC