RSS Forums RSS

How to use typedef for the template structure? What its syntax?

Please support our C++ advertiser: Programming Forums
Reply
Posts: 53
Reputation: Rhohitman is an unknown quantity at this point 
Solved Threads: 4
Rhohitman's Avatar
Rhohitman Rhohitman is offline Offline
Junior Poster in Training

How to use typedef for the template structure? What its syntax?

  #1  
Oct 10th, 2008
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..
Last edited by Rhohitman : Oct 10th, 2008 at 1:34 am. Reason: mistakely pressed tab and enter.. as it was summited without completion
AddThis Social Bookmark Button
Reply With Quote  
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: How to use typedef for the template structure? What its syntax?

  #2  
Oct 10th, 2008
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.
Reply With Quote  
Posts: 53
Reputation: Rhohitman is an unknown quantity at this point 
Solved Threads: 4
Rhohitman's Avatar
Rhohitman Rhohitman is offline Offline
Junior Poster in Training

Re: How to use typedef for the template structure? What its syntax?

  #3  
Oct 10th, 2008
Originally Posted by dougy83 View Post
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
Chazing Dreams ;'P
Shhhh.......ZZzzzzzzzzzzzzzzzzzzzz.....
Reply With Quote  
Posts: 2,000
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 331
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: How to use typedef for the template structure? What its syntax?

  #4  
Oct 10th, 2008
What's a problem?
  1. typedef box<int> MyFavouriteBoxType;
  2. ...
  3. MyFavouriteBoxType b1;
Look at STL headers on you C++ installation. There are lots of (useful) typedefs in STL templates...
Reply With Quote  
Posts: 86
Reputation: gregorynoob is an unknown quantity at this point 
Solved Threads: 5
gregorynoob gregorynoob is offline Offline
Junior Poster in Training

Re: How to use typedef for the template structure? What its syntax?

  #5  
Oct 10th, 2008
yea, you gotta either macro it or typedef with specifying the actual template type 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:
  1. typedef my_monumental_structure<another_huge_type>::iterator sit;
and then i just sit... all the way... rocks
Last edited by gregorynoob : Oct 10th, 2008 at 10:23 am.
Reply With Quote  
Posts: 53
Reputation: Rhohitman is an unknown quantity at this point 
Solved Threads: 4
Rhohitman's Avatar
Rhohitman Rhohitman is offline Offline
Junior Poster in Training

Re: How to use typedef for the template structure? What its syntax?

  #6  
Oct 10th, 2008
Originally Posted by ArkM View Post
What's a problem?
  1. typedef box<int> MyFavouriteBoxType;
  2. ...
  3. 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.
Last edited by Rhohitman : Oct 10th, 2008 at 11:59 am.
Chazing Dreams ;'P
Shhhh.......ZZzzzzzzzzzzzzzzzzzzzz.....
Reply With Quote  
Posts: 53
Reputation: Rhohitman is an unknown quantity at this point 
Solved Threads: 4
Rhohitman's Avatar
Rhohitman Rhohitman is offline Offline
Junior Poster in Training

Re: How to use typedef for the template structure? What its syntax?

  #7  
Oct 10th, 2008
Originally Posted by gregorynoob View Post
yea, you gotta either macro it or typedef with specifying the actual template type 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:
  1. 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.
Can you kindly explain it..
Last edited by Rhohitman : Oct 10th, 2008 at 12:51 pm.
Chazing Dreams ;'P
Shhhh.......ZZzzzzzzzzzzzzzzzzzzzz.....
Reply With Quote  
Posts: 2,000
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 331
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: How to use typedef for the template structure? What its syntax?

  #8  
Oct 10th, 2008
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"?
Reply With Quote  
Posts: 273
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Solved Threads: 42
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: How to use typedef for the template structure? What its syntax?

  #9  
Oct 10th, 2008
I believe he's trying to do something like:
  1. typedef box MyGreatBeautifulBox;
  2.  
  3. //and use it as:
  4. MyGreatBeutifulBox<int> a;
  5. MyGreatBeutifulBox<char>b;

I don't think it can be done
Reply With Quote  
Posts: 2,000
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 331
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: How to use typedef for the template structure? What its syntax?

  #10  
Oct 10th, 2008
Hehe...
  1. template<typename T>
  2. struct MyGreatBeautifulBox:box<T>{};
  3.  
  4. int main()
  5. {
  6. MyGreatBeautifulBox<int> ibox;
  7. MyGreatBeautifulBox<char> cbox;
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Other Threads in the C++ Forum
Views: 1553 | Replies: 12 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 12:29 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC