在 C++ 中将类公开给其他类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4792614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:31:41  来源:igfitidea点击:

Making classes public to other classes in C++

c++public

提问by Simplicity

If I have two classes for example as follows:

如果我有两个类,例如如下:

class A {...}

class B {...}

If I want to make class Apublic to class B, do I just make the members of class Apublic or I can just use public class A {...}?

如果我想将班级A公开给班级B,我是只公开班级的成员A还是我可以使用public class A {...}

Is there a way to tell class Bfor example that only class Ais public for you? In other words, can I make public classes to Aprotected or private to others? Or, this is just a matter of deriving a class (inheritance)?

有没有办法告诉班级B,例如只有班级A对你来说是公开的?换句话说,我可以将公共类设为A受保护或私有给他人吗?或者,这只是派生类(继承)的问题?

Thanks.

谢谢。

回答by Patrick

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.

如果您在包含文件(.h 文件)中定义您的类,那么您将公开您的类。包含这个包含文件的每个其他源文件都会知道这个类,并且可以有一个指向它的指针。

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.

如果这个定义被放在一个包含文件中,每个其他的源都可以引用(有一个指向)这个类,但是由于类中的所有方法都是私有的,没有其他源可以构造它、破坏它、设置它的值或得到它的价值。

You make the contents of a class public by putting methods from it in the 'public' part of the class definition, like this:

您可以通过将类中的方法放入类定义的“公共”部分来公开类的内容,如下所示:

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++ 中没有办法只将类的一部分公开给一组有限的其他类。所以,如果你让另一个类成为朋友,它就可以访问所有私有方法。因此,限制好友的数量。

回答by Puppy

You can use friendship.

你可以使用友谊。

class A { friend class B; private: int x; };
class B { B() { A a; a.x = 0; // legal };

回答by BatchyX

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();
};