本文介绍了使用C ++的实例级封装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个部分的问题。首先,我理解C ++只提供类级数据封装,意味着同一类的所有对象都可以访问彼此的私有成员。我了解原因,但发现了一些链接(例如),这似乎与这一点矛盾,表明我可以做以下:

I have a two-part question. First, I understand that C++ provides only class-level data encapsulation, meaning that all objects of the same class have access to one another's private members. I understand the reason for this, but have found some links (i.e. http://www.programmerinterview.com/index.php/c-cplusplus/whats-the-difference-between-a-class-variable-and-an-instance-variable/) which appear to contradict this point, suggesting that I could do the following:

class testclass {
  private:

  // Below would be an instance-level variable, and new memory for it is set aside 
  // in each object I create of class testclass 
  int x;

  // Below would be a class-level variable, memory is set aside only once no matter 
  // how many objects of the same class 
  static int y;
}

我想做的是让这项工作,喜欢在每个实例化(这是我的第二个问题)中的私有类中定义一个变量。由于上面的代码片段似乎没有实现这一点,是否有一个工作,我可以使用创建个人对象的私有数据?谢谢!

What I would like to do is actually make this work, i.e., I would like to define a variable in a class which is private in each instantiation (this is my second question). Since the code snippet above does not appear to achieve this, is there a work around I can use to create data that is private to individual objects? Thank you!

编辑:

这是真的,我还在学习OO基础。我将使用无处不在的汽车示例来显示我想要做什么,我确信必须是一个常见的东西尝试。欢迎任何有关如何重新思考的建议:

It's true that I'm still learning OO basics. I'll use the ubiquitous car example to show what I'm trying to do, which I'm sure must be a common thing to try. I'd welcome any suggestions for how to rethink it:

class car {
  private:
    int mileage;
  public:
    car(int); // Constructor
    void odometer();
};

car::car(int m) {
  mileage = m;
}

void car::odometer() {
  return mileage;
}

int main(void) {
  car ford(10000), honda(20000);

  cout<<ford.odometer(); //Returns 20000, since honda constructor overwrites private variable 'mileage'
}

推荐答案

Priviledge(public,private,protected)仅适用于名称。只有在解析名称的时间,编译器才会应用权限。一旦编译,所有这些信息都消失了。

Priviledge (public, private, protected) only applies to names. Only during the time when a name is resolved will the compiler apply permissions. Once compiled, all such information is gone.

在上面的例子中,所有使用的名称x和y在一个范围内,解析为THOSE变量将是私有的类。只有在您的类中声明的函数,无论是否为静态,都将能够按名称访问这些变量。

In your example above, all uses of the names x and y within a scope that resolves to THOSE variables will be private to your class. Only functions declared in your class, be they static or not, will be able to access those variables by name.

如果你把变量赋给其他对象,然后可以通过其他具有其他权限的名字引用变量。

All bets are off however if you give out the variable to other objects that can then refer to the variable by other names which have other permissions.

我不知道你在问什么引用在每个实例化。 AFAIK,没有本地方法使一个变量私有,只有那个实例可以访问它。在所有情况下,实例可以访问彼此的私有部分。

I'm not sure what you're asking with reference to "in each instantiation". AFAIK, there is no native way to make a variable private such that only that instance can access it. In all cases, instances can access each other's private parts.

有一些方法可以绕过这个,我想。首先是模板化你的类,并给每个实例一个不同的类型。你可以使用一个整数模板参数或某事。这可能会使生活烦人,虽然当你尝试使用这些类型作为同一种事情。你必须虚拟化,并有一个抽象的基类或某事。

There's some ways you could get around this I suppose. First is to templatize your class and give each instance a different type. You could do this with an integer template parameter or something. This could make life annoying though as you try to work with these types as the same kind of thing. You'd have to virtualize and have an abstract base class or something.

目前,这是我能想到的唯一的方法。

Currently that's the only method I can think of. All others depend on calling entities playing nice.

一般来说,你希望保护成员免受其他实例的影响。相同类型的通常情况下传递到相同类型是在复制和分配期间,您基本上需要所有关于源正确复制的知识。我的打赌是,你需要重新思考你想做什么,无论是什么。

Generally speaking it's rare that you'd want to protect members from other instances. The usual case of the same type being passed to the same type is during copy and assignment, where you basically need all knowledge about the source to correctly copy. My bet is that you need to rethink what you're trying to do, whatever that is.

这篇关于使用C ++的实例级封装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 23:55