当多态遇见对象数组会发生什么?
#include <cstdlib>
#include <iostream>
using namespace std;
class Parent
{
protected:
int i;
public:
virtual void f()
{
cout<<"Parent::f"<<endl;
}
};
class Child : public Parent
{
protected:
int j;
public:
Child(int i, int j)
{
this->i = i;
this->j = j;
}
void f()
{
cout<<"i = "<<i<<" "<<"j = "<<j<<endl;
}
};
int main(int argc, char *argv[])
{
Parent* p = NULL;
Child* c = NULL;
Child ca[3] = {Child(1, 2), Child(3, 4), Child(5, 6)};
p = ca;
c = ca;
cout<<hex<<p+1<<endl;
p->f();
c->f();
p++;
c++;
p->f(); //此处导致崩溃!
c->f();
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}
不要将多态应用于数组
指针运算是通过指针的类型进行的
多态通过虚函数表实现的
多态与指针运算玩混搭的后果
结论:
不要在数组上使用多态!!
为什么没有讲解多重继承?
C++在语法上直接支持多重继承
#include <cstdlib>
#include <iostream>
using namespace std;
class P1{
protected:
int i;
};
class P2{
protected:
int j;
};
class Child : public P1, public P2{ //多继承
public:
Child(int i, int j){
this->i = i;
this->j = j;
}
void print(){
cout << "i = " << i<< " "<< "j = " << j<<endl;
}
};
int main(int argc, char *argv[])
{
Child c(1,2);
c.print();
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}
被实际开发经验抛弃的多继承
工程开发中真正意义上的多继承是几乎不被使用的
多重继承带来的代码复杂性远多于其带来的便利
多重继承对代码维护性上的影响是灾难性的
在设计方法上,任何多继承都可以用单继承代替
多继承复杂性示例
#include <cstdlib>
#include <iostream>
using namespace std;
class Object
{
protected:
int d;
public:
void f()
{
cout<<"Object::f"<<endl;
}
};
class P1 : public Object
{
protected:
int i;
};
class P2 : public Object
{
protected:
int j;
};
class Child : public P1, public P2
{
public:
Child(int i, int j)
{
this->d = 0; //error,存在二义性
this->i = i;
this->j = j;
}
void print()
{
cout<<"i = "<<i<<" "<<"j = "<<j<<endl;
}
};
int main(int argc, char *argv[])
{
Child c(1, 2);
c.print();
c.f(); //error,存在二义性
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}
为什么多继承复杂?
在只有单继承的系统中,类之间的继承关系为一颗树 。
在引入多重继承的系统中,类之间的继承关系呈现为一张图。【数据结构中的图示非常复杂的】
C++中对多继承二义性的解决方案
虚继承:
为了解决从不同途径继承来的同名数据成员造成的二义性问题,可以将共同基类设置为虚基类。 这时从不同的路径继承过来的同名数据成员在内存中就只有一个拷贝
#include <cstdlib>
#include <iostream>
using namespace std;
class Object{ //虚基类
protected:
int d;
public:
void f(){
cout << "Object::f()" <<endl;
}
};
class P1 : virtual public Object{ //虚继承
protected:
int i;
};
class P2 : virtual public Object{ //虚继承
protected:
int j;
};
class Child : public P1, public P2{ //多继承
public:
Child(int i, int j){
this->i = i;
this->j = j;
this->d = 0; //只存在一个拷贝
}
void print(){
cout << "i = " << i<< " "<< "j = " << j<<endl;
}
};
int main(int argc, char *argv[])
{
Child c(1,2);
c.print();
c.f();
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}
C++是否有Java中接口的概念?
绝大多数面向对象语言都不支持多继承
绝大多数面向对象语言都支持接口的概念
C++中没有接口的概念
C++中可以使用纯虚函数实现接口
实际工程经验证明
多重继承接口不会带来二义性和复杂性等问题
多重继承可以通过精心设计用单继承和接口来代替
tips:
接口类只是一个功能说明,而不是功能实现。
子类需要根据功能说明定义功能实现。
接口使用示例
#include <cstdlib>
#include <iostream>
using namespace std;
class Interface1 //接口1
{
public:
virtual void print() = 0;
virtual int add(int i, int j) = 0;
};
struct Interface2 //接口2
{
virtual int add(int i, int j) = 0;
virtual int minus(int i, int j) = 0;
};
class Child : public Interface1, public Interface2 //多继承 接口
{
public:
void print()
{
cout<<"Child::print"<<endl;
}
int add(int i, int j) //实现
{
return i + j;
}
int minus(int i, int j)
{
return i - j;
}
};
int main(int argc, char *argv[])
{
Child c;
c.print();
cout<<c.add(3, 5)<<endl;
cout<<c.minus(4, 6)<<endl;
cout << endl;
Interface1* i1 = &c; //c++赋值兼容性原则
Interface2* i2 = &c;
cout<<i1->add(7, 8)<<endl;
cout<<i2->add(7, 8)<<endl; //两种效果相同
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}