有两个类foo和bar,bar为foo服务我希望设计成foo 与 foo::bar , 而不是 foo 与 bar
类似std::vector<int>::iterator 和 std::vector
应该怎样把bar的class放在foo里面?
业精于勤,荒于嬉;行成于思,毁于随。
You can use class nesting or typedef. The vector::iterator you mentioned is a type alias defined by typedef
class Out { public: class Inside { }; }; Out some1; Out::Inside some2;
class A { }; class B { public: typedef A Inside; }; A some1; B some2; B::Inside some3;
C++ supports nested classes, such as
class foo { class bar { /* ... */ }; /* ... */ };
or
class foo { class bar; /* ... */ }; class foo::bar { /* ... */ };
For specific usage, please refer to C++ Primer 5ed. section 19.5 Nested Classes.
In addition, std::vector<int>::iterator is an alias of another class in std::vector (typedef), not a nested class of std::vector.
std::vector<int>::iterator
std::vector
typedef
You can use class nesting or typedef. The vector::iterator you mentioned is a type alias defined by typedef
C++ supports nested classes, such as
or
For specific usage, please refer to C++ Primer 5ed. section 19.5 Nested Classes.
In addition,
std::vector<int>::iterator
is an alias of another class instd::vector
(typedef
), not a nested class ofstd::vector
.