typedef = typedef (class member) ... how?

Started by
6 comments, last by dacredens 19 years, 10 months ago
ive made a typedef ... and theres an instance of it in a class of mine. is it possible to have a different instance of my typedef "=" or equal the class->typedef (for example) ie class { typedef a; } typedef b = class->typedef a; because it thinks im saying typedef b = class help!?
Advertisement
I'm not quite exactly sure what your talking about, but you can't access a typedef from an instace and you can't typedef a typedef from an instance but you can typedef a typedef thats inside a class like so:

class foo {public:   typedef int bar;   typedef bar foobars;   const bar& getBar() const;   bar& getBar();};typedef foo::bar foobar;int main() {   foo f;   foo::bar s = f.getBar();   foobar g = f.getBar();   return 0;}


[edited by - snk_kid on June 6, 2004 7:54:47 AM]
typedef != type

In C++ you don''t have type-variables. Typedef lets you use a custom typeNAME.

typedef ;
For example:
typedef int myINT;

--
You''re Welcome,
Rick Wong
- Google | Google for GameDev.net | GameDev.net''s DirectX FAQ. (not as cool as the Graphics and Theory FAQ)
Does anyone know why they (C++ comittee / inventors / whatever they''re called) choose to call it typedef.
Wouldn''t it have been more clear if they would have named it: alias ?

eg. why not:
alias int myINT;

quote:Original post by Direct4D
Does anyone know why they (C++ comittee / inventors / whatever they''re called) choose to call it typedef.
Wouldn''t it have been more clear if they would have named it: alias ?

eg. why not:
alias int myINT;



Two bad words: C compatability.
Not giving is not stealing.
"alias" also implies something rather more powerful, along the lines of what #define offers you. Typedefs are just that - type definitions. "The type of something labelled as ''x'' is y."

So yes, OP, your class doesn''t have an "instance of the typedef", it has a member variable which is typed according to that typedef.

I assume what you want to do is something like
typedef FOO int;class fooholder {  public:  FOO a;}fooholder myFoo;FOO b = myFoo->a; // a and b are now FOOs (ints) which are equal


Otherwise, you''re probably rather confused and need to think more about what exactly you''re trying to do.

Are you trying to somethig like Runtime Type Info (RTTI)? u cannot store a type to then create an instance of an object from. Maybe what your trying to do is something like what the Factory Pattern is for?

[edited by - samgzman on June 6, 2004 6:11:43 PM]
This kind of thing is done all the time in generic programming using templates. A prime example is in the C++ Standard Library.

In a Container class there will be typedefs for the types contained in the class, the iterators, pointers, references and others so that they can be consistently refered to by name. I often will make my own typedef based upon those in a container to make code clearer and easier to modify. e.g.

#include <vector>#include <iostream>struct Score {    int value;    Score(int score) : value(score) {    }};std::ostream operator<<(std::ostream& os, const Score& score) {    os << score.value;    return os;}int main() {    //I'm going to be using the vector class and cout object so need to bring them in from the std namespace    using std::vector;    using std::cout;    typedef vector<Score> ScoreCont;		//Container of Scores    typedef ScoreCont::iterator ScoreContItr;   //Iterator for a container of Scores. Uses iterator typedef from the vector class    //use the typedefs    ScoreCont scores;    scores.push_back(Score(3));    scores.push_back(Score(19));    scores.push_back(Score(7));    scores.push_back(Score(47));    ScoreContItr it = scores.begin();    ScoreContItr end = scores.end();    for(;it != end; ++it) {        cout << *it << "\n";    }    return 0;}


For example if you decided to use a deque instead of a vector you could change the typedef for ScoreCont and, because deque also has the iterator type defined, the other typedef for the iterator will automatically work.

[edited by - quorn on June 7, 2004 4:32:14 AM]

This topic is closed to new replies.

Advertisement