在C ++中,是为派生类创建的一组新的私有变量吗?(In C++, are a new set of private variables created for a derived class?)
这可能是一个愚蠢的问题,但我一整天都在考虑这个问题。 创建派生类时,是否使用基类的公共成员对其自己的私有变量进行操作? 为了更好地解释这一点,我在下面写了一个快速程
#include <iostream> #include <string> class person { public: std::string getname(void) { return name; } void setname(std::string x) { name = x; } private: std::string name; int age; }; class doubleperson :public person{}; int main() { person a, b; doubleperson c; a.setname("bob"); b.setname("jon"); c.setname("jim"); std::cout << a.getname() << std::endl; std::cout << b.getname() << std::endl; std::cout << c.getname() << std::endl; std::cout << &c.getname() << std::endl; std::cout << &a.getname() << std::endl; std::cout << &b.getname() << std::endl; std::cin.get();当我运行此代码时,我得到了
bob jon jim 002F904 002F8E0 002F8BC这告诉我“name”变量有三个独立的实例。 当我创建一个基类时,c.setname是否在类person或class double person中使用setname,它如何知道从我的派生类而不是基类中对“name”进行操作?
This might be a stupid question but I've been thinking about it all day. When a derived class is created, is it using public members of the base class to act on its own private variables? To explain this better I wrote a quick program below
#include <iostream> #include <string> class person { public: std::string getname(void) { return name; } void setname(std::string x) { name = x; } private: std::string name; int age; }; class doubleperson :public person{}; int main() { person a, b; doubleperson c; a.setname("bob"); b.setname("jon"); c.setname("jim"); std::cout << a.getname() << std::endl; std::cout << b.getname() << std::endl; std::cout << c.getname() << std::endl; std::cout << &c.getname() << std::endl; std::cout << &a.getname() << std::endl; std::cout << &b.getname() << std::endl; std::cin.get();When I run this code I get
bob jon jim 002F904 002F8E0 002F8BCWhich tells me that there are three separate instances of the "name" variable. When I create a base class, does c.setname use setname in class person or class double person and how does it know to act on "name" from my derived class instead of the base class?
最满意答案
您将访问权限与构建类的实例混淆。
声明类时,您将定义新类型的结构。 这可以包括成员变量(和函数等),并且每次将类创建为对象时都会创建这些变量。 例如,如果您创建了个不同的Person类变量,则每个变量对于在类定义中声明的每个成员变量都有自己的不同内存。 这有例外,例如静态成员变量,但通常每个类的实例都有自己的每个成员变量的内存空间。
创建子类只是将现有类有效地扩展为可包含新成员变量(和其他相关因素)的新类型。 父类的原始内存定义仍包含在子类中。 在这种情况下,doubleperson类包含与父person类相同的信息,即每个doubleperson类现在都有一个名为“name”的成员变量。 因此,当您创建每个doubleperson类的实例时,您将在内存中为此成员变量名称和doubleperson类的其他部分创建一个不同的位置。 这当然是您在每个实例中看到不同内存位置的原因。 如果你已经在person类中声明了成员变量名静态,那么现在可以在doubleperson(和person)的所有实例之间共享一个变量,然后你会看到每个doubleperson实例具有相同的内存位置for成员变量名称。
这也就是说成员变量的访问权限不反映它们如何存储在类定义中。 Access只定义类层次结构中的哪个级别,您可以访问特定的成员变量。 这里,name变量可以在person类的函数内访问,但不能在doubleperson子类中访问。 但是,无论何时创建doubleperson类的实例,都要将person类的定义与它一起放在自己独特的内存空间中。 要修改特定成员变量在实例中跨内存存储的方式,您需要查看“静态”或其他编程模式等关键字来实现此类功能。
You're confusing access privledges with building an instance of a class.
When you declare a class, you are defining the structure of a new type. This can include member variables (and functi, etc.), and these would be created each time the class is created as an object. For example, if you created different variables of class Person, each of these would have their own distinct memory for each of the member variables declared in the class definition. There are excepti to this, such as static member variables, but in general each instance of a class will have its own memory space of each of its member variables.
Creating a subclass is just effectively extending an existing class into a new type that can include new member variables (and other related factors). The original memory definition of the parent class is still contained within the subclass. In this case, your doubleperson class contains the same information as the parent person class, namely each doubleperson class now has a member variable called "name". Hence, when you create an instance of each doubleperson class, you create a distinct location in memory for this member variable name and the other parts of the doubleperson class. This is of course why you see distinct memory locati in each of the instances. If you had declared the member variable name in the person class static, this would now make one variable shared across all instances of doubleperson (and person, for that matter), and then you would have seen each doubleperson instance having the same memory location for the member variable name.
This is also to say access privlidges of member variables is not reflective of how they are stored in a class definition. Access just defines at what level in the class hierarchy you can access a particular member variable. Here, the name variable can be accessed within functi of the person class but not the doubleperson subclass. But whenever you create an instance of the doubleperson class, you carry the definition of the person class along with it in its own distinct memory space. To modify how a particular member variable is stored in memory across instances, you need to look to keywords like "static" or other programming patterns to implement this type of functionality.
#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格
推荐阅读
留言与评论(共有 14 条评论) |
本站网友 科技小知识 | 12分钟前 发表 |
std | |
本站网友 设备搬迁 | 21分钟前 发表 |
and these would be created each time the class is created as an object. For example | |
本站网友 信息加密 | 17分钟前 发表 |
the name variable can be accessed within functi of the person class but not the doubleperson subclass. But whenever you create an instance of the doubleperson class | |
本站网友 硝酸甘油软膏 | 4分钟前 发表 |
cout << &a.getname() << std | |
本站网友 什么护手霜好 | 11分钟前 发表 |
本站网友 蓝芒 | 28分钟前 发表 |
您可以访问特定的成员变量 | |
本站网友 李洋洋 | 28分钟前 发表 |
本站网友 宜昌房屋出租 | 3分钟前 发表 |
但通常每个类的实例都有自己的每个成员变量的内存空间 | |
本站网友 95042 | 8分钟前 发表 |
cout << &c.getname() << std | |
本站网友 吴建豪前女友 | 24分钟前 发表 |
它如何知道从我的派生类而不是基类中对“name”进行操作? This might be a stupid question but I've been thinking about it all day. When a derived class is created | |
本站网友 长春御翠豪庭 | 16分钟前 发表 |
std | |
本站网友 味捷 | 5分钟前 发表 |
声明类时 | |
本站网友 河东租房 | 17分钟前 发表 |