943,708 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 7142
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 10th, 2008
0

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

Expand Post »
I create the simple template structure like this
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. template <class info>
  4. struct box
  5. {
  6. info data;
  7. };
  8.  
  9. int main()
  10. {
  11. box<int> b1;
  12. b1.data = 1;
  13. std::cout<<b1.data;
  14. return 0;typedef box square;
  15. }

Now i want to use the typedef for this template structure.
I could not figure out the syntax...
as i tried
C++ Syntax (Toggle Plain Text)
  1. typedef box square;

But i didn't workout... Please help me..
Last edited by Rhohitman; Oct 10th, 2008 at 2:34 am. Reason: mistakely pressed tab and enter.. as it was summited without completion
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
Rhohitman is offline Offline
81 posts
since Dec 2007
Oct 10th, 2008
0

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.
Reputation Points: 85
Solved Threads: 45
Posting Whiz in Training
dougy83 is offline Offline
275 posts
since Jun 2007
Oct 10th, 2008
0

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

Click to Expand / Collapse  Quote originally posted by dougy83 ...
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
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
Rhohitman is offline Offline
81 posts
since Dec 2007
Oct 10th, 2008
0

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

What's a problem?
C++ Syntax (Toggle Plain Text)
  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...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Oct 10th, 2008
0

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 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:
c++ Syntax (Toggle Plain Text)
  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 11:23 am.
Reputation Points: 12
Solved Threads: 5
Junior Poster in Training
gregorynoob is offline Offline
86 posts
since Jun 2008
Oct 10th, 2008
0

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

Click to Expand / Collapse  Quote originally posted by ArkM ...
What's a problem?
C++ Syntax (Toggle Plain Text)
  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 12:59 pm.
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
Rhohitman is offline Offline
81 posts
since Dec 2007
Oct 10th, 2008
0

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 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:
c++ Syntax (Toggle Plain Text)
  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 1:51 pm.
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
Rhohitman is offline Offline
81 posts
since Dec 2007
Oct 10th, 2008
0

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"?
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Oct 10th, 2008
0

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

I believe he's trying to do something like:
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 110
Solved Threads: 43
Posting Whiz in Training
Sci@phy is offline Offline
279 posts
since Sep 2008
Oct 10th, 2008
0

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

Hehe...
C++ Syntax (Toggle Plain Text)
  1. template<typename T>
  2. struct MyGreatBeautifulBox:box<T>{};
  3.  
  4. int main()
  5. {
  6. MyGreatBeautifulBox<int> ibox;
  7. MyGreatBeautifulBox<char> cbox;
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: number of ways to..ehh, i hate counting ways
Next Thread in C++ Forum Timeline: Using c++ to send email with smtp





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC