If I have two classes for example as follows:
如果我有两个类,例如:
class A {...}
class B {...}
If I want to make class A
public to class B
, do I just make the members of class A
public or I can just use public class A {...}
?
如果我想将A类公开给B类,我是将A类的成员公开,还是只使用A类{…}?
Is there a way to tell class B
for example that only class A
is public for you? In other words, can I make public classes to A
protected or private to others? Or, this is just a matter of deriving a class (inheritance)?
有没有办法告诉B类,例如只有a类对你是公共的?换句话说,我可以将公共类设置为受保护类还是私有类?或者,这只是一个派生类(继承)的问题?
Thanks.
谢谢。
3 个解决方案
#1
16
There's a substantial difference between making the class public and making its contents public.
使班级公众和使其内容公开是有很大区别的。
If you define your class in an include file (.h file) then you are making your class public. Every other source file that includes this include file will know about this class, and can e.g. have a pointer to it.
如果在包含文件中定义类(。然后你将你的类公开。包括文件在内的所有其他源文件都知道这个类,并且可以有一个指向它的指针。
The only way to make a class private, it to put its definition in a source (.cpp) file.
使类成为私有的惟一方法是将其定义放在源文件(.cpp)中。
Even when you make a class public, you don't necessarily have to make the contents of your class public. The following example is an extreme one:
即使是在公开课的时候,你也不一定要让课堂的内容公开。下面的例子是一个极端的例子:
class MyClass
{
private:
MyClass();
~MyClass();
void setValue(int i);
int getValue() const;
};
If this definition is put in an include file, every other source can refer to (have a pointer to) this class, but since all the methods in the class are private, no other source may construct it, destruct it, set its value or get its value.
如果这个定义放在一个include文件中,其他所有的源都可以引用(有指针指向)这个类,但是由于类中的所有方法都是私有的,所以没有其他源可以构造它、销毁它、设置它的值或获取它的值。
You make the contents of a class public by putting methods from it in the 'public' part of the class definition, like this:
通过将类的方法放在类定义的“public”部分,将类的内容公开,如下所示:
class MyClass
{
public:
MyClass();
~MyClass();
int getValue() const;
private:
void setValue(int i);
};
Now everybody may construct and destruct instances of this class, and may even get the value. Setting the value however, is not public, so nobody is able to set the value (except the class itself).
现在,每个人都可以构造并销毁该类的实例,甚至可以得到值。但是,设置值不是公共的,因此没有人能够设置值(除了类本身)。
If you want to make the class public to only some other class of your application, but not to the complete application, you should declare that other class a friend, e.g.:
如果你想将类公开给应用程序的其他类,而不是完整的应用程序,你应该声明其他类为朋友,例如:
class SomeOtherClass;
class MyClass
{
friend SomeOtherClass;
public:
MyClass();
~MyClass();
int getValue() const;
private:
void setValue(int i);
};
Now, SomeOtherClass may access all the private methods from MyClass, so it may call setValue to set the value of MyClass. All the other classes are still limited to the public methods.
现在,SomeOtherClass可以访问MyClass中的所有私有方法,因此它可以调用setValue来设置MyClass的值。所有其他类仍然仅限于公共方法。
Unfortunately, there is no way in C++ to make only a part of your class public to a limited set of other classes. So, if you make another class a friend, it is able to access all private methods. Therefore, limit the number of friends.
不幸的是,c++中不可能只将类的一部分公开给一组有限的其他类。因此,如果将另一个类作为朋友,它就能够访问所有私有方法。因此,限制朋友的数量。
#2
3
You can use friendship.
您可以使用友谊。
class A { friend class B; private: int x; };
class B { B() { A a; a.x = 0; // legal };
#3
3
If B has a strong interdependence to A, i suggest you use a nested class. Fortunately, nested class can be protected or private.
如果B与a有很强的依赖性,我建议您使用嵌套类。幸运的是,嵌套类可以被保护或私有。
class A {
protected:
// the class A::B is visible from A and its
// inherited classes, but not to others, just
// like a protected member.
class B {
public:
int yay_another_public_member();
};
public:
int yay_a_public_member();
};
#1
16
There's a substantial difference between making the class public and making its contents public.
使班级公众和使其内容公开是有很大区别的。
If you define your class in an include file (.h file) then you are making your class public. Every other source file that includes this include file will know about this class, and can e.g. have a pointer to it.
如果在包含文件中定义类(。然后你将你的类公开。包括文件在内的所有其他源文件都知道这个类,并且可以有一个指向它的指针。
The only way to make a class private, it to put its definition in a source (.cpp) file.
使类成为私有的惟一方法是将其定义放在源文件(.cpp)中。
Even when you make a class public, you don't necessarily have to make the contents of your class public. The following example is an extreme one:
即使是在公开课的时候,你也不一定要让课堂的内容公开。下面的例子是一个极端的例子:
class MyClass
{
private:
MyClass();
~MyClass();
void setValue(int i);
int getValue() const;
};
If this definition is put in an include file, every other source can refer to (have a pointer to) this class, but since all the methods in the class are private, no other source may construct it, destruct it, set its value or get its value.
如果这个定义放在一个include文件中,其他所有的源都可以引用(有指针指向)这个类,但是由于类中的所有方法都是私有的,所以没有其他源可以构造它、销毁它、设置它的值或获取它的值。
You make the contents of a class public by putting methods from it in the 'public' part of the class definition, like this:
通过将类的方法放在类定义的“public”部分,将类的内容公开,如下所示:
class MyClass
{
public:
MyClass();
~MyClass();
int getValue() const;
private:
void setValue(int i);
};
Now everybody may construct and destruct instances of this class, and may even get the value. Setting the value however, is not public, so nobody is able to set the value (except the class itself).
现在,每个人都可以构造并销毁该类的实例,甚至可以得到值。但是,设置值不是公共的,因此没有人能够设置值(除了类本身)。
If you want to make the class public to only some other class of your application, but not to the complete application, you should declare that other class a friend, e.g.:
如果你想将类公开给应用程序的其他类,而不是完整的应用程序,你应该声明其他类为朋友,例如:
class SomeOtherClass;
class MyClass
{
friend SomeOtherClass;
public:
MyClass();
~MyClass();
int getValue() const;
private:
void setValue(int i);
};
Now, SomeOtherClass may access all the private methods from MyClass, so it may call setValue to set the value of MyClass. All the other classes are still limited to the public methods.
现在,SomeOtherClass可以访问MyClass中的所有私有方法,因此它可以调用setValue来设置MyClass的值。所有其他类仍然仅限于公共方法。
Unfortunately, there is no way in C++ to make only a part of your class public to a limited set of other classes. So, if you make another class a friend, it is able to access all private methods. Therefore, limit the number of friends.
不幸的是,c++中不可能只将类的一部分公开给一组有限的其他类。因此,如果将另一个类作为朋友,它就能够访问所有私有方法。因此,限制朋友的数量。
#2
3
You can use friendship.
您可以使用友谊。
class A { friend class B; private: int x; };
class B { B() { A a; a.x = 0; // legal };
#3
3
If B has a strong interdependence to A, i suggest you use a nested class. Fortunately, nested class can be protected or private.
如果B与a有很强的依赖性,我建议您使用嵌套类。幸运的是,嵌套类可以被保护或私有。
class A {
protected:
// the class A::B is visible from A and its
// inherited classes, but not to others, just
// like a protected member.
class B {
public:
int yay_another_public_member();
};
public:
int yay_a_public_member();
};