[英]Any solution or programming tips for Inner class?
I'm having some toubt here. 我在这里有些陶醉。 Hope you guys can share out some programming tips.
希望你们能分享一些编程技巧。 Just curious to know whether is it a good programming practice if I do something like the code below.
如果我做类似下面的代码,只是想知道这是否是一个好的编程习惯。
class Outer {
public:
class Inner {
public:
Inner() {}
}
Outer() {}
};
I have been doing this for structure where I only want my structure to be expose to my class instead of global. 我一直在为结构执行此操作,在该结构中 ,我只希望将结构公开给我的类,而不是全局的。 But the case is different here, I am using a class now?
但是这里的情况有所不同,我现在正在使用一个类吗? Have you guys facing such a situation before?
你们以前遇到过这样的情况吗? Very much appreciated on any advice from you ;)
非常感谢您的任何建议;)
I'll break the answer into two parts: 我将答案分为两部分:
This is a good practice in many cases. 在许多情况下,这是一个好习惯。 Here's one where we implement a link list:
这是我们实现链接列表的地方:
template <class T>
class MyLinkList {
public:
class Node {
public:
Node* next;
T data;
Node(const T& data, Node* node) : next(node), data(data) {}
};
class Iterator {
public:
Node* current;
Iterator(Node* node) : current(node) {}
T& operator*() { return current->data; }
void operator++(int) { current = current->next; }
bool operator!=(int) { return current != NULL; }
};
private:
Node* head;
} }
The above is just snippet that is not intended to be complete or compilable. 上面的代码段并不完整或不可编译。 The point is to show that Node and Iterator are inner classes to the MyLinkList class.
关键是要表明Node和Iterator是MyLinkList类的内部类。 The reason why this makes sense is to convey the fact that Node and Iterator are not independent to be stand alone by themselves, but they need to be qualified by MyLinkList (for instance MyLinkList::Iterator it )
之所以这样做,是因为它传达了一个事实,即Node和Iterator并非独立于彼此独立,而是需要由MyLinkList限定(例如MyLinkList :: Iterator it )。
This is purely a matter of style, however I think it is typically more common in the C++ community to use a namespace named detail
for classes that are purely helpers or are purely used in the implementation of other classes. 这纯粹是样式问题,但是我认为在C ++社区中,通常将名称为
detail
的命名空间用于纯辅助类或纯用于其他类的类中,这是更常见的。 There are several advantages to using namespaces in place of inner classes, among them include: greater compatibility (how compilers resolve names in inner classes can be incredibly different between Visual C++ and GCC, for example), more encapsulation (in the inner/outer variant, the inner class has greater access to members of instances of the outer class), easier implementation (you don't have to fully qualify the helper class every single time in the implementation file, since you can put a using directive in the ".cpp" source file). 使用命名空间代替内部类有很多优点,其中包括:更大的兼容性(例如,编译器解析内部类中的名称的方式在Visual C ++和GCC之间可能会有难以置信的差异),更多的封装(内部/外部变体) ,内部类可以更方便地访问外部类实例的成员),实现起来更容易(您不必每次都在实现文件中完全限定helper类,因为您可以将using指令放在“中。 cpp”源文件)。 If you are going to use an inner class, then you need to make the conscious decision to make that a part of your API.
如果要使用内部类,则需要做出明智的决定以使其成为API的一部分。
Using Namespaces
使用命名空间
namespace collection
{
namespace detail
{
class LinkedListNode
{
//...
};
}
class LinkedList
{
// ...
};
}
Using Inner Classes
使用内部类
namespace collection
{
class LinkedList
{
// ...
class LinkedListNode
{
// ...
};
// ...
};
}
It's not an everyday thing, but it's not unheard of, either. 这不是日常事务,但这也不是闻所未闻的。 You would do this if there were a class (Inner) that only makes sense to a client program when the client is using Outer.
如果存在一个类(内部),该类仅在客户端使用外部时才对客户端程序有意义,则可以执行此操作。
If you only want a class to be exposed in a certain file, you can use an unnamed namespace within that file. 如果只希望在某个文件中公开类,则可以在该文件中使用未命名的名称空间。 Then whatever code is within that namespace is only available within that file.
然后,该命名空间中的任何代码仅在该文件中可用。
namespace
{
//stuff
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.