[英]Overriding in inheritance in C++
class X {
int i;
public:
X() { i = 0; }
void set(int ii) { i = ii; }
int read() const { return i; }
int permute() { return i = i * 47; }
};
Above is the definition of class X 以上是X类的定义
Another class Y is there as 另一个Y类在那里
class Y : public X {
int i; // Different from X's i
public:
Y() { i = 0; }
int change() {
i = permute(); // Different name call
return i;
}
void set(int ii) {
i = ii;
X::set(ii); // Same-name function call
}
};
My doubt is that class X also consists of a variable named i and it is been inherited by class Y, but i of class Y should overwrite it, but the size of class(Y) is coming 8. 我的疑问是,类X也包含一个名为i的变量,并且被类Y继承,但是类Y的i应该覆盖它,但是类(Y)的大小即将到8。
Secondly, for the line 其次,对于行
X::set(ii)
Can we call the function like this? 我们可以这样调用函数吗? Is this function of class X invoked for any object?
是否为任何对象调用了X类的此函数?
Many many thanks in advance 非常感谢
Y::i
doesn't override anything (you can only override a virtual function). Y::i
不会覆盖任何内容(您只能覆盖虚拟函数)。 It hides X::i
, so there are two different i
s, one in the base class and one in the derived. 它隐藏了
X::i
,所以有两个不同的i
,一个在基类中,一个在派生类中。
To your second question, outside of the class you can only use syntax like X::set(ii);
第二个问题,在类之外,您只能使用
X::set(ii);
这样的语法X::set(ii);
when set
is a static
member function, not a normal or virtual
member function. 当
set
为static
成员函数时,不是普通或virtual
成员函数。 Inside the class you can use it to force a particular class' definition of the member function to be used. 在类内部,您可以使用它来强制使用特定类对成员函数的定义。
Edit: I should probably answer the tricky (somewhat related) question: if the static type differs from the dynamic type, which i
is used? 编辑:我可能应该回答棘手的(有点相关)问题:如果静态类型与动态类型不同,那么
i
会使用哪种类型? For example, let's consider a simplified version: 例如,让我们考虑一个简化的版本:
class base {
protected: // we'll make `i` protected, so `derived` can access it if necessary.
int i;
public:
base() : i(0) {}
void hide() { i = 2; }
virtual void set() { i = 10; }
};
class derived : public base {
int i;
public:
derived() : i(0) {}
void hide() { i = 1; }
void set() { i = 5; }
};
Now, since set
is virtual, the call in main
is to derived::set
. 现在,由于
set
是虚拟的,因此main
的调用是derived::set
。 Since hide
is not virtual, the call in main
will be to base::hide()
. 由于
hide
不是虚拟的,因此main
的调用将是对base::hide()
的调用。 The question is, which class' i
will each of them assign to? 问题是,
i
将每个人分配给哪个班级?
The answer is fairly simple: even when the function is virtual, the variable is not, so each function refers to the variable in its own class. 答案很简单:即使函数是虚拟的,变量也不是,因此每个函数都在其自己的类中引用该变量。 Having/lacking
virtual
controls which function you call, but not which variable is referred to by that function. 有/缺少
virtual
控件,您可以调用哪个函数,但不能由该函数引用哪个变量。
Please note that there is no overriding in your code, only name hiding . 请注意,您的代码中没有覆盖 ,只有名称隐藏 。
My doubt is that class X also consists of a variable named i and it is been inherited by class Y, but i of class Y should overwrite it, but the size of class(Y) is coming 8.
我的疑问是,类X也包含一个名为i的变量,并且被类Y继承,但是类Y的i应该覆盖它,但是类(Y)的大小即将到8。
Inheritance does not overwrite variables. 继承不会覆盖变量。 The
i
in class Y
will hide the name i
from class X
, but both will exist, hence the size of 8 . Y
类中的i
将隐藏X
类中的名称i
,但两者都将存在,因此大小为8 。
X::set(ii) Can we call the function like this?
X :: set(ii)我们可以这样调用函数吗? Is this function of class X invoked for any object?
是否为任何对象调用了X类的此函数?
From within class Y
, this qualified call request that the set
from X
is invoked in a static way instead of the set
of Y
. 从类中
Y
,那这个合格的呼叫请求set
从X
中,而不是静态的方式调用set
的Y
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.