c++ - 如何设计将一个类放在另一个类名下
迷茫
迷茫 2017-04-17 13:26:19
0
2
348

有两个类foo和bar,bar为foo服务
我希望设计成
foo 与 foo::bar , 而不是 foo 与 bar

类似
std::vector<int>::iterator 和 std::vector

应该怎样把bar的class放在foo里面?

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(2)
巴扎黑

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;
Peter_Zhu

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template