Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

dacredens

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

This topic is 5274 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
"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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!