繁体   English   中英

将类类型成员定义为公共和私有之间有什么区别?

[英]what's difference between defining a class type member as public and private?

class Screen{
public:
      typedef std::string::size_type pos;
private:
      pos cursor = 0;
      pos height = 0, width = 0;
      std::string contents;
};

我试过构造Screen的实例。 都不能访问pos 顺便说一句,我知道访问控制。 我只是想知道类型成员的访问控制的含义。

公共意味着任何人(其他类)都可以接触这些变量。 私有意味着只有您(您的方法)可以触摸这些变量。

有一个警告,引入了“朋友”,它给不是您的函数或类提供了访问所有变量的权限。

成员类型和成员变量以相同的方式工作。 除了类的方法和朋友之外,私有类型将不可用,就像私有方法或变量一样。 相反,任何人都可以访问公共类型,方法或变量。 与(非静态)方法和变量不同,类型必须是静态的。

例如,由于pos是私有的,因此将无法编译。

#include <string>
class Test {
  private:
    using pos = std::string::size_type;
  public:

};

int main(int argc, char *argv[])
{
  Test::pos a = 5;

  return 0;
}

要从屏幕类的外部访问pos类型,请首先将typedef移至公共可访问性。 然后它将与screen::pos ,就像使用静态方法或变量一样。

以下应编译就好了,但(可能警告有关a未被使用,但是这是一个不同的问题。)

#include <string>
class Test {
  public:
    using pos = std::string::size_type;    
};

int main(int argc, char *argv[])
{
  Test::pos a = 5;

  return 0;
}

您可能对此有些疯狂,甚至在内部将整个类定义为公共或私有。

因为尽管您的typedef是公开的,但实际的成员却被声明为私有的。 有关更多信息,请参见C ++类访问说明符

资料来源: https : //www.tutorialspoint.com/cplusplus/cpp_class_access_modifiers.htm

公众成员

公共成员可以从班级外部但在计划内的任何地方访问。 您可以设置和获取公共变量的值,而无需任何成员函数,如以下示例所示:

码:

#include <iostream>

using namespace std;

class Line {
   public:
      double length;
      void setLength( double len );
      double getLength( void );
};

// Member functions definitions
double Line::getLength(void) {
   return length ;
}

void Line::setLength( double len) {
   length = len;
}

// Main function for the program
int main() {
   Line line;

   // set line length
   line.setLength(6.0); 
   cout << "Length of line : " << line.getLength() <<endl;

   // set line length without member function
   line.length = 10.0; // OK: because length is public
   cout << "Length of line : " << line.length <<endl;

   return 0;
}

上面代码的输出:

Length of line : 6
Length of line : 10

私人会员:

私有成员变量或函数无法访问,甚至无法从类外部查看。 只有班级和朋友功能可以访问私有成员。

默认情况下,一个类的所有成员都是私有成员,例如,在以下类中,宽度是私有成员,这意味着在标记成员之前,将假定该成员为私有成员:

class Box {
   double width;

   public:
      double length;
      void setWidth( double wid );
      double getWidth( void );
};

实际上,我们在私有部分定义数据,而在公共部分定义相关功能,以便可以从类外部调用它们,如以下程序所示。

#include <iostream>

using namespace std;

class Box {
   public:
      double length;
      void setWidth( double wid );
      double getWidth( void );

   private:
      double width;
};

// Member functions definitions
double Box::getWidth(void) {
   return width ;
}

void Box::setWidth( double wid ) {
   width = wid;
}

// Main function for the program
int main() {
   Box box;

   // set box length without member function
   box.length = 10.0; // OK: because length is public
   cout << "Length of box : " << box.length <<endl;

   // set box width without member function
   // box.width = 10.0; // Error: because width is private
   box.setWidth(10.0);  // Use member function to set it.
   cout << "Width of box : " << box.getWidth() <<endl;

   return 0;
}

以上代码的输出:

Length of box : 10
Width of box : 10

来自class.access#4

因为访问控制适用于名称,所以如果访问控制适用于typedef名称,则仅考虑typedef名称本身的可访问性。 考虑由typedef引用实体的可访问性。

class A {
  class B { };
public:
  typedef B BB;
};

void f() {
  A::BB x;          // OK, typedef name A​::​BB is public
  A::B y;           // access error, A​::​B is private
}

因此,这并不意味着您的类型是公共的,将使成员变量具有公共访问权限。 typedef用于命名类型, 而不用于指定成员的访问权限。

类型成员不同于数据成员。 它更像是静态的东西,我无法通过实例访问它。 因此,将其定义为公共还是私有决定我是否可以通过Screen::pos进行访问。 而已。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM