c++11 - What's the difference between type and name in C++? -
i reading this stack overflow question, , added constructor code question follwing,
class foo { struct bar { int i; bar(int = 5) :i(a) {}; }; public: bar baz() { return bar(); } }; int main() { foo f; // foo::bar b = f.baz(); // error auto b = f.baz(); // ok std::cout <<"b.i="<< b.i<<endl; return 0; } the code outputs b.i=5. in question concludes name of private not accessible type is. what's difference between type , name, generally?
and have 2 specific scenarios.
what's difference between following 2 declarations? why can output
b.i=5auto b = f.baz();?foo::bar b = f.baz(); auto b = f.baz();if add
typedef bar b;in public part offoo, difference between following?foo::bar b = f.baz(); foo::b b = f.baz();
if there difference between scenario 1 , 2?
what difference between type , name
a type has none, 1 or several names. typedef and alias means of creating new name type.
public , private keywords relate names not underlying types or members.
in order explicitly declare object of particular type need name type. auto doesn't need this. if example use unnamed class as return type, class has no name auto can still used on it.
an type have @ 1 'true name'. when using through typedef or alias, compiler uses under name (or raw version of name). so:
class {}; typedef b; std::cout << typeid(b).name(); prints "class a". unnamed object cannot given 'true name'. however, when using typedef , decltype. new name can created. if name used create objects. typeid().name print newly assigned name. if object wasn't unnamed start 'true name' name printed.
the scenarios:
the difference in first using privately declared name. illegal. has how different ways of type deduction work. scott meyers explains here. since public function call provides type, return type public. however,
baron not public. it's small difference, reason.
decision has been made in design. makes sense, want struct used when return it.the same goes here. there no difference,
foo::barinaccessible.
edit
can give example type has none names? unnamed union in above comment example of this?
as described here use lambda function follows:
auto f = [] () -> struct {int x, y ; } { return { 99, 101 } ; } ; without using auto or decltype there no way of creating variable f. since it's type has no name. example of type without name.
struct foo { struct{ int x; int y; } membervar; }; would allow following:
foo bar; auto baz = bar.membervar; std::cout << baz.x; which results in bunch of initialized stuff of course, idea:). type of membervar here unnamed. making impossible define baz explicitly.
and int considered name type of int?
int bit special, being fundamental type. 'int' indeed name of type int. it's no means name, int32_t, example name exact same type on compilers(on other systems int16_t equivalent int).
std::cout << typeid(int32_t).name(); prints "int".
notes:
i've refrained using alias indicator other names of object, since might cause confusion alias keyword.
most of have gathered experience. might have missed bits.
by lack better word have used expression 'true name'. if knows official or better word i'd happy hear it:).
Comments
Post a Comment