I just learned about typedef. Suppose I have an instance:
private:
typedef std::string int doubles abc;
when I make an accessors to instance abc:
returnType get(){...}
what should I put in the returnType? is it abc or the data type? thx
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I’m not sure what you wanted to do here. Your
typedefis invalid syntax, and “doubles” is no C++ type (but probably just a typo). Examples for valid syntax would be:You can then use the
typedefin function signatures, just like other types:Although you should put the
typedefin thepublicpart of your class if you want to use it in public member functions. And you should be aware that code outside of your class will always have to prefix thetypedefwith the name of your class, unless it istypedef‘d again.Apart from that:
typedefdoes not provide any way to have a variable represent more than one type. C++ is a statically typed language, so this is not directly possible. You can, however:unionand some discriminator to store what type it is currently storingvoid*and casting – Not recommended!boost::variant,boost::anyetc. like suggested by othersEdit: Finally, on your use of the term “instance”: It is usally used to refer to an instance of a class, i.e. a particular object belonging to a class. What you mean is a “member variable”.