what is the best way to put a container class or a some other class inside a class as private or a public member?
将容器类或其他类作为私有或公共成员放入类中的最佳方法是什么?
Requirements:
1.Vector< someclass> inside my class
1.班里的
2.Add and count of vector is needed interface
2.矢量的添加和计数是需要的接口
6 个解决方案
#1
1
If the container's state is part of the class's invariant, then it should, if possible, be private.
如果容器的状态是类的不变量的一部分,那么如果可能的话,它应该是私有的。
For example, if the container represents a three dimensional vector then part of the invariant might be that it always contains exactly 3 numbers. Exposing it as a public member would allow code external to the class to change the containers size, which in turn could cause problems for any routine which requires the container's size to be constant. Keeping the container private limits the places in your software where the container's size can be modified to the class's member functions.
例如,如果容器表示三维向量,则不变量的一部分可能是它总是包含正好3个数字。将其公开为公共成员将允许类外部的代码更改容器大小,这反过来可能导致任何需要容器大小恒定的例程的问题。保持容器的私密性限制了软件中可以将容器大小修改为类的成员函数的位置。
#2
1
Whether a member is declared Private or Public depends entirely on your application. Could you give some more detail?
会员被宣布为私人会员还是公共会员,完全取决于您的申请。你能提供更多细节吗?
One important point to remember when declaring your member is that if you provide a "getter" to retrieve it, then you are no longer encapsulating that object. Instead, it can be good to write wrapper methods exposing only the functionality you wish to expose.
在声明你的成员时要记住的一个要点是,如果你提供一个“getter”来检索它,那么你就不再封装那个对象了。相反,编写仅暴露您希望公开的功能的包装器方法可能会很好。
For example, with a Vector member, you might write an AddItem and Clear method, if that's all the functionality you wish to expose.
例如,对于Vector成员,您可以编写AddItem和Clear方法,如果这是您希望公开的所有功能。
#3
1
Since you're talking about a class, I think it should be private. If you want it to be public, rather create a struct - to make it obvious that you want the members variables to be used.
既然你在谈论一堂课,我认为它应该是私人的。如果您希望它是公共的,那么创建一个结构 - 显然您希望使用成员变量。
A viable alternative to exposing the vector
member is creating a visitor function (or an internal iterator). This way you obey the law of Demeter better:
暴露向量成员的可行替代方法是创建访问者函数(或内部迭代器)。这样你就更好地遵守得墨忒耳的规律了:
class ContWrapper {
std::vector<int> _ints;
public:
class Action {
public:
virtual void accept( int i ) = 0;
};
void each_int( Action& a );
};
Also be very careful when exporting e.g. an std::vector<T>
from a library, too: the client code might not use the same STL implementation as you did, so the layout of these member variables may differ!
输出时也要非常小心,例如来自库的std :: vector
#4
0
Make all members private and use accessor methods, this allows you to change the implementation later. Only in very unusual circumstances would I make any data member public.
使所有成员都是私有的并使用访问器方法,这允许您稍后更改实现。只有在非常特殊的情况下才会公开任何数据成员。
Remember that chaning the implementation happens more often than you may imagine, its not just a case of changing the type of the container but maybe you want to change the mechanism. Say you were storing names in a list, after a while you may chose to index this list with a hash and would like to have the hash updated every time you add a new name. If your implementation is suitably encapsulated doing this is easy, if you have just exposed the vector you would need to make changes that will adjust the interface (and so the change will ripple out).
请记住,实现的操作比您想象的更频繁,它不仅仅是更改容器类型的情况,而是您可能想要更改机制。假设您将名称存储在列表中,过了一段时间,您可能选择使用哈希对此列表编制索引,并希望每次添加新名称时都更新哈希值。如果您的实现被适当地封装,这很容易,如果您刚刚暴露了向量,则需要进行更改以调整接口(因此更改将会消失)。
If this is new to new you have a read of: http://en.wikipedia.org/wiki/Encapsulation_(classes_-_computers)
如果这是新手,请阅读:http://en.wikipedia.org/wiki/Encapsulation_(classes_-_computers)
#5
0
There is a third way - sometimes it is better to inherit from the container and override it's methods to achieve your goal (for example thread safety). Anyway, making it public almost always isn't a good idea.
还有第三种方法 - 有时候最好从容器继承并覆盖它的方法来实现你的目标(例如线程安全)。无论如何,将它公开几乎总是不是一个好主意。
#6
0
Considering that you want to encapsulate the container inside another class implies that it cannot be public, and also the public methods of your class should not expose anything implementation-specific about the container. That way the implementation of your class (i.e. the container) can be changed without changing its interface.
考虑到您希望将容器封装在另一个类中意味着它不能是公共的,并且类的公共方法也不应该暴露任何特定于实现的容器。这样,可以在不更改其界面的情况下更改类(即容器)的实现。
#1
1
If the container's state is part of the class's invariant, then it should, if possible, be private.
如果容器的状态是类的不变量的一部分,那么如果可能的话,它应该是私有的。
For example, if the container represents a three dimensional vector then part of the invariant might be that it always contains exactly 3 numbers. Exposing it as a public member would allow code external to the class to change the containers size, which in turn could cause problems for any routine which requires the container's size to be constant. Keeping the container private limits the places in your software where the container's size can be modified to the class's member functions.
例如,如果容器表示三维向量,则不变量的一部分可能是它总是包含正好3个数字。将其公开为公共成员将允许类外部的代码更改容器大小,这反过来可能导致任何需要容器大小恒定的例程的问题。保持容器的私密性限制了软件中可以将容器大小修改为类的成员函数的位置。
#2
1
Whether a member is declared Private or Public depends entirely on your application. Could you give some more detail?
会员被宣布为私人会员还是公共会员,完全取决于您的申请。你能提供更多细节吗?
One important point to remember when declaring your member is that if you provide a "getter" to retrieve it, then you are no longer encapsulating that object. Instead, it can be good to write wrapper methods exposing only the functionality you wish to expose.
在声明你的成员时要记住的一个要点是,如果你提供一个“getter”来检索它,那么你就不再封装那个对象了。相反,编写仅暴露您希望公开的功能的包装器方法可能会很好。
For example, with a Vector member, you might write an AddItem and Clear method, if that's all the functionality you wish to expose.
例如,对于Vector成员,您可以编写AddItem和Clear方法,如果这是您希望公开的所有功能。
#3
1
Since you're talking about a class, I think it should be private. If you want it to be public, rather create a struct - to make it obvious that you want the members variables to be used.
既然你在谈论一堂课,我认为它应该是私人的。如果您希望它是公共的,那么创建一个结构 - 显然您希望使用成员变量。
A viable alternative to exposing the vector
member is creating a visitor function (or an internal iterator). This way you obey the law of Demeter better:
暴露向量成员的可行替代方法是创建访问者函数(或内部迭代器)。这样你就更好地遵守得墨忒耳的规律了:
class ContWrapper {
std::vector<int> _ints;
public:
class Action {
public:
virtual void accept( int i ) = 0;
};
void each_int( Action& a );
};
Also be very careful when exporting e.g. an std::vector<T>
from a library, too: the client code might not use the same STL implementation as you did, so the layout of these member variables may differ!
输出时也要非常小心,例如来自库的std :: vector
#4
0
Make all members private and use accessor methods, this allows you to change the implementation later. Only in very unusual circumstances would I make any data member public.
使所有成员都是私有的并使用访问器方法,这允许您稍后更改实现。只有在非常特殊的情况下才会公开任何数据成员。
Remember that chaning the implementation happens more often than you may imagine, its not just a case of changing the type of the container but maybe you want to change the mechanism. Say you were storing names in a list, after a while you may chose to index this list with a hash and would like to have the hash updated every time you add a new name. If your implementation is suitably encapsulated doing this is easy, if you have just exposed the vector you would need to make changes that will adjust the interface (and so the change will ripple out).
请记住,实现的操作比您想象的更频繁,它不仅仅是更改容器类型的情况,而是您可能想要更改机制。假设您将名称存储在列表中,过了一段时间,您可能选择使用哈希对此列表编制索引,并希望每次添加新名称时都更新哈希值。如果您的实现被适当地封装,这很容易,如果您刚刚暴露了向量,则需要进行更改以调整接口(因此更改将会消失)。
If this is new to new you have a read of: http://en.wikipedia.org/wiki/Encapsulation_(classes_-_computers)
如果这是新手,请阅读:http://en.wikipedia.org/wiki/Encapsulation_(classes_-_computers)
#5
0
There is a third way - sometimes it is better to inherit from the container and override it's methods to achieve your goal (for example thread safety). Anyway, making it public almost always isn't a good idea.
还有第三种方法 - 有时候最好从容器继承并覆盖它的方法来实现你的目标(例如线程安全)。无论如何,将它公开几乎总是不是一个好主意。
#6
0
Considering that you want to encapsulate the container inside another class implies that it cannot be public, and also the public methods of your class should not expose anything implementation-specific about the container. That way the implementation of your class (i.e. the container) can be changed without changing its interface.
考虑到您希望将容器封装在另一个类中意味着它不能是公共的,并且类的公共方法也不应该暴露任何特定于实现的容器。这样,可以在不更改其界面的情况下更改类(即容器)的实现。