在C ++中将类公开给其他类 [英] Making classes public to other classes in C++

查看:144
本文介绍了在C ++中将类公开给其他类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

If I have two classes for example as follows:

class A {...}

class B {...}



如果我想让类 A public to class B ,do I just make the members of class A public or我可以使用 public class A {...}

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 {...}?

c $ c> 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)?

谢谢。

推荐答案

将类公开和公开其内容之间有很大的区别。

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.

使一个类为私有的唯一方法,它把它的定义放在源代码(.cpp)文件中。

The only way to make a class private, it to put its definition in a source (.cpp) file.

即使你把一个类公开,你也不一定要公开你的类的内容。以下示例是一个极端的示例:

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.

通过将类中的方法放在类定义的public部分中,public类的内容,如下所示:

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

现在,SomeOtherClass可以访问MyClass中的所有私有方法,因此可以调用setValue来设置值的MyClass。所有其他类仍然限于公共方法。

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.

不幸的是,C ++中没有办法只让一部分类公开给有限的其他类。所以,如果你让另一个类成为朋友,它能够访问所有私有方法。因此,限制朋友的数量。

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 ++中将类公开给其他类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆