使用下面显示的代码我创建了4个类(3个子类和一个超类),每个类都有相同的print_info()函数。我已经从每个类创建了指针对象,并将它们插入到使用超类创建的向量中。
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
using namespace std;
class Vehicle
{
private:
int wheels, max_speed;
string model;
public:
virtual void print_info()
{
cout << "This is function print_info() in Vehicle (parent class)" << endl;
}
};
class Car: public Vehicle
{
private:
int doors, passengers;
public:
void print_info()
{
cout << "This is function print_info() in Car (child class)" << endl;
}
};
class Bike: public Vehicle
{
private:
string owner;
bool hasLights;
public:
string my_owner();
};
class Truck: public Vehicle
{
private:
float cargo_weight;
int allowed_speed;
public:
void print_info()
{
cout << "This is function print_info() in Truck (child class)" << endl;
}
};
int main()
{
Vehicle *v1 = new Vehicle();
Vehicle v2;
Car c1;
Car c2;
Bike b1;
Truck t1;
Truck t2;
vector <Vehicle> vvec;
Vehicle *v = new Vehicle();
Car *c = new Car();
Bike *b = new Bike();
Truck *t = new Truck();
vvec.push_back(*v);
vvec.push_back(*c);
vvec.push_back(*b);
vvec.push_back(*t);
vector<Vehicle>::iterator iter = vvec.begin();
while( iter != vvec.end())
{
iter->print_info();
iter++;
}
v1 = &v2;
v1->print_info();
v1 = &c2;
v1->print_info();
v1 = &t2;
v1->print_info();
system("pause");
return 0;
}
想要的输出是:
这是Vehicle(父类)
中的函数print_info这是Car(子类)中的函数print_info
这是Vehicle(父类)
中的函数print_info这是Truck(子类)中的函数print_info
这是Vehicle(父类)
中的函数print_info这是Car(子类)中的函数print_info
这是Truck(子类)中的函数print_info
相反,我得到了:
这是Vehicle(父类)
中的函数print_info这是Vehicle(父类)
中的函数print_info这是Vehicle(父类)
中的函数print_info这是Vehicle(父类)
中的函数print_info这是Vehicle(父类)
中的函数print_info这是Car(子类)中的函数print_info
这是Truck(子类)中的函数print_info
我认为这是由于早期绑定,但我不知道如何解决这个问题!
答案 0 :(得分:3)
你需要创建指针vector <Vehicle*> vvec;
的向量,当像vvec.push_back(*v);
这样的push_back按值传递元素并且它转换为Vehicle
时,所以不会有多态性,因为vector的所有元素都只是简单的Vehicle对象,改为:
//...
vector <Vehicle*> vvec;
Vehicle *v = new Vehicle();
Car *c = new Car();
Bike *b = new Bike();
Truck *t = new Truck();
vvec.push_back(v);
vvec.push_back(c);
vvec.push_back(b);
vvec.push_back(t);
vector<Vehicle*>::iterator iter = vvec.begin();
while( iter != vvec.end())
{
(*iter)->print_info();
iter++;
}
//...
您的输出将是:
This is function print_info() in Vehicle (parent class)
This is function print_info() in Car (child class)
This is function print_info() in Vehicle (parent class)
This is function print_info() in Truck (child class)
This is function print_info() in Vehicle (parent class)
This is function print_info() in Car (child class)
This is function print_info() in Truck (child class)
答案 1 :(得分:2)
这是object slicing的典型示例。
当您取消引用指针以将其插入向量long[] arr = new long[list.size()];
for (int i=0; i < list.size(); i++) {
arr[i] = list.get(i);
}
时,将其转换为基类类型。因此,它现在表现为“车辆”。
如果您想获得预期的行为,请尝试使用vvec.push_back(*v)
和vector <Vehicle*> vvec
。
答案 2 :(得分:1)
你想要的是多态性,所以你必须存储Vehicle引用或Vehicle指针。而且,由于您无法在vector
中存储引用,因此指针是最佳选择。
vector <Vehicle*> vvec;
然后只需按下指针,调用print_info
就会触发多态行为
vvec.push_back(v);
vvec.push_back(c);
vvec.push_back(b);
vvec.push_back(t);
vector<Vehicle*>::iterator iter = vvec.begin();
while( iter != vvec.end())
{
(*iter)->print_info();
iter++;
}