对覆盖现象的困惑
Confusion about the phenomenon of overriding
我对什么时候说函数被重写有点困惑。
My book says "When a derived class creates a function with the same return type and signature as a
member function in the base class, but with a new implementation, it is said to be overriding
that function."
我很清楚书上说的是什么,但我的问题是,如果我只保留相同的名称并更改 return 类型和签名,我可以说该功能被覆盖了还是有任何不同的术语?
就像我尝试了以下代码:
#include <iostream>
using namespace std;
class Mammal
{
public:
int Move() const
{
cout << "mammal moving\n";
return 1;
}
void Move(int x) const
{
cout << "walked " << x << " steps\n";
}
};
class Dog : public Mammal
{
public:
void Move()
{
cout << "Dog moving\n";
}
};
int main()
{
Dog Moti;
Moti.Move();
//Moti.Move(2); error: no matching function for call to 'Dog::Move(int)'
cout << endl;
return 0;
}
Output : Dog moving
所以我在 Dog
中有 Move() 方法,但它的 return 类型和签名与 Mammal
中的 Move() 方法不同。那么说 Dog
中的 Move() 已覆盖 Mammal
中的 Move() 是否正确?为什么我有这种困惑是因为当我调用 Moti.Move(2)
时,我收到了错误。这意味着 Mammal
中的 void Move(int x) const
被隐藏了。但是当baseclass中的一个重载方法在derivedclass中被覆盖时,就会出现'hiding'的现象。所以这就是为什么我认为“Dog 中的 Move() 覆盖了 Mammal 中的 Move()”,但根据书籍定义,这似乎是错误的。
我做了一些互联网搜索,发现“在虚函数的情况下,return 类型必须相同或协变”,但我的问题不是关于虚函数或任何东西。我只是很困惑我可以说上面的代码中发生了覆盖还是有任何不同的术语。
上面代码中的问题让我想起了 CPP 的一个非常有趣的概念,Name Hiding 简单地说,如果我们试图重载派生的方法 class,那么编译器将隐藏基 class 方法,除非我们明确通知编译器包含基 class 方法,或者我们使用范围解析运算符 [=] 显式调用基 class 方法10=]
查看一些相关文章:
https://bastian.rieck.me/blog/posts/2016/name_hiding_cxx/
overloading base class method in derived class
https://www.geeksforgeeks.org/g-fact-89/
P.S: 我们不能重载基于 return 类型的方法,如果我们尝试这样做会出错。
我对什么时候说函数被重写有点困惑。
My book says "When a derived class creates a function with the same return type and signature as a member function in the base class, but with a new implementation, it is said to be overriding that function."
我很清楚书上说的是什么,但我的问题是,如果我只保留相同的名称并更改 return 类型和签名,我可以说该功能被覆盖了还是有任何不同的术语? 就像我尝试了以下代码:
#include <iostream>
using namespace std;
class Mammal
{
public:
int Move() const
{
cout << "mammal moving\n";
return 1;
}
void Move(int x) const
{
cout << "walked " << x << " steps\n";
}
};
class Dog : public Mammal
{
public:
void Move()
{
cout << "Dog moving\n";
}
};
int main()
{
Dog Moti;
Moti.Move();
//Moti.Move(2); error: no matching function for call to 'Dog::Move(int)'
cout << endl;
return 0;
}
Output : Dog moving
所以我在 Dog
中有 Move() 方法,但它的 return 类型和签名与 Mammal
中的 Move() 方法不同。那么说 Dog
中的 Move() 已覆盖 Mammal
中的 Move() 是否正确?为什么我有这种困惑是因为当我调用 Moti.Move(2)
时,我收到了错误。这意味着 Mammal
中的 void Move(int x) const
被隐藏了。但是当baseclass中的一个重载方法在derivedclass中被覆盖时,就会出现'hiding'的现象。所以这就是为什么我认为“Dog 中的 Move() 覆盖了 Mammal 中的 Move()”,但根据书籍定义,这似乎是错误的。
我做了一些互联网搜索,发现“在虚函数的情况下,return 类型必须相同或协变”,但我的问题不是关于虚函数或任何东西。我只是很困惑我可以说上面的代码中发生了覆盖还是有任何不同的术语。
上面代码中的问题让我想起了 CPP 的一个非常有趣的概念,Name Hiding 简单地说,如果我们试图重载派生的方法 class,那么编译器将隐藏基 class 方法,除非我们明确通知编译器包含基 class 方法,或者我们使用范围解析运算符 [=] 显式调用基 class 方法10=]
查看一些相关文章:
https://bastian.rieck.me/blog/posts/2016/name_hiding_cxx/
overloading base class method in derived class
https://www.geeksforgeeks.org/g-fact-89/
P.S: 我们不能重载基于 return 类型的方法,如果我们尝试这样做会出错。