不同类型继承中的 C++ new 关键字
C++ new keyword in inheritance with different types
我最近开始学习 OOP。如果这是一个菜鸟问题,请原谅我。我的问题是,
我认为 new 关键字仅用于相同的数据类型,例如:
char* p = new char; // OR
int* myArr = new int[i] //etc...
在研究继承和虚函数时,我遇到了这个:
#include <iostream>
using namespace std;
class Human {
public:
virtual void className() {
cout << "Human" << endl;
}
};
class Asian : public Human {
public:
void className() {
cout << "Asian" << endl;
}
};
int main() {
Human* h1 = new Asian();
h1->className();
}
在main函数中,我们用基数class初始化指针,然后new关键字后有派生的class?这两种数据类型代表什么,我应该如何使用它们?
这就是使用OOP的优势。假设你有一家汽车厂,你正在生产轿车、两厢车等。它们都是汽车,所以它们有四个轮子、发动机等。为了不多次出现相同的数据,可以使用继承。父项和子项 class 都可以相互转换,但您可以举个例子来理解,假设您有一辆 class 并改装了一辆汽车。由于我没有指定汽车的类型,它可以是轿车或两厢车,而不是有 2 个不同的功能,你可以有一个采用父对象(汽车)的功能。
virtual
关键字指定函数的子版本应该 运行 而不是父版本。所以在你的例子中,因为我们正在制作一个 Asian
对象,我们将调用 className
的儿童(亚洲)版本。如果没有 virtual
关键字,那么父版本将 运行.
请注意,在您的情况下,class Human
可访问 class Asian
和
If a base class is accessible, one can implicitly convert a pointer to a derived class to a pointer to that base class (4.10, 4.11). [ Note: it follows that members and friends of a class X can implicitly convert an X* to a pointer to a private or protected immediate base class of X. —end note ]
根据上面引用的语句,您可以将指向派生 class Asian
的指针转换为指向基 class Human
的指针](意思是从Asian*
到Human*
),也就是你写的时候做的:
Human* h1 = new Asian();
在上面的语句中,在左侧我们有一个类型为 Human
的名为 h1
的指针,在右侧通过使用我们正在创建的关键字 new
堆上 Asian
类型的对象。但请注意,使用 new
具有 second 效果,即在堆上创建类型 Asian
的对象之后,然后 pointer返回那个对象。所以本质上,
在右侧,我们得到一个指向 Asian
(Asian*).
的指针
正如我已经在答案开头解释(引用)的那样,您可以将指向 Asian
的指针转换为指向 Human
的指针,这就是这里发生的事情。
现在当你写道:
h1->className();
您必须考虑 3 个重要事实:
className
是一个 虚拟 function/method
h1
是指针
Asian
公开继承Human
并根据 virtual function documentation
a virtual function is a member function you may redefine for other derived classes, and can ensure that the compiler will call the redefined virtual function for an object of the corresponding derived class, even if you call that function with a pointer or reference to a base class of the object.
因此结果是名为 className
的派生 class' 函数在 运行-time 被调用,我们看到 Asian
打印在控制台上。
我最近开始学习 OOP。如果这是一个菜鸟问题,请原谅我。我的问题是, 我认为 new 关键字仅用于相同的数据类型,例如:
char* p = new char; // OR
int* myArr = new int[i] //etc...
在研究继承和虚函数时,我遇到了这个:
#include <iostream>
using namespace std;
class Human {
public:
virtual void className() {
cout << "Human" << endl;
}
};
class Asian : public Human {
public:
void className() {
cout << "Asian" << endl;
}
};
int main() {
Human* h1 = new Asian();
h1->className();
}
在main函数中,我们用基数class初始化指针,然后new关键字后有派生的class?这两种数据类型代表什么,我应该如何使用它们?
这就是使用OOP的优势。假设你有一家汽车厂,你正在生产轿车、两厢车等。它们都是汽车,所以它们有四个轮子、发动机等。为了不多次出现相同的数据,可以使用继承。父项和子项 class 都可以相互转换,但您可以举个例子来理解,假设您有一辆 class 并改装了一辆汽车。由于我没有指定汽车的类型,它可以是轿车或两厢车,而不是有 2 个不同的功能,你可以有一个采用父对象(汽车)的功能。
virtual
关键字指定函数的子版本应该 运行 而不是父版本。所以在你的例子中,因为我们正在制作一个 Asian
对象,我们将调用 className
的儿童(亚洲)版本。如果没有 virtual
关键字,那么父版本将 运行.
请注意,在您的情况下,class Human
可访问 class Asian
和
If a base class is accessible, one can implicitly convert a pointer to a derived class to a pointer to that base class (4.10, 4.11). [ Note: it follows that members and friends of a class X can implicitly convert an X* to a pointer to a private or protected immediate base class of X. —end note ]
根据上面引用的语句,您可以将指向派生 class Asian
的指针转换为指向基 class Human
的指针](意思是从Asian*
到Human*
),也就是你写的时候做的:
Human* h1 = new Asian();
在上面的语句中,在左侧我们有一个类型为 Human
的名为 h1
的指针,在右侧通过使用我们正在创建的关键字 new
堆上 Asian
类型的对象。但请注意,使用 new
具有 second 效果,即在堆上创建类型 Asian
的对象之后,然后 pointer返回那个对象。所以本质上,
在右侧,我们得到一个指向 Asian
(Asian*).
正如我已经在答案开头解释(引用)的那样,您可以将指向 Asian
的指针转换为指向 Human
的指针,这就是这里发生的事情。
现在当你写道:
h1->className();
您必须考虑 3 个重要事实:
className
是一个 虚拟 function/methodh1
是指针Asian
公开继承Human
并根据 virtual function documentation
a virtual function is a member function you may redefine for other derived classes, and can ensure that the compiler will call the redefined virtual function for an object of the corresponding derived class, even if you call that function with a pointer or reference to a base class of the object.
因此结果是名为 className
的派生 class' 函数在 运行-time 被调用,我们看到 Asian
打印在控制台上。