第19节 经典问题解析二
-------------------------------------资源来源于网络,仅供自学使用,如有侵权,联系我必删.
第一:
当多态遇见对象数组会发生什么
#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;//父类指针 p
Child* c = NULL;//子类指针 c
Child ca[3] = {Child(1, 2), Child(3, 4), Child(5, 6)};//子类对象数组
p = ca;//将一个子类子类第一个对象的地址赋值给一个指针
c = ca;//子类给子类 赋值
cout<<hex<<p+1<<endl;
p->f();//Child(1, 2)
c->f();//Child(1, 2)
p++;//p++; <--> p = p+1; <--> (unsigned int)p + 1*sizeof(*p) <--> p + 1 指向的8 个字节不是合法的对象
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 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;//d不知道是P1和P2
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();//f不知道是P1和P2
this->i = i;
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}
C++ 中对多继承二义性的解决方案
虚继承: :
为了解决从不同途径继承来的同名数据成员造成的二义性问题 , 可以将共同基类设置为虚基类 。 这时从不同的路径继承过来的同名数据成员在内存中就只有一个拷贝 。
第三
C++ 是否有 Java 中接口的概念 ?
? 绝大多数面向对象语言都不支持多继承
? 绝大多数面向对象语言都支持 接口 的概念
? C++ 中没有接口的概念
? C++ 中可以使用纯虚函数实现接口
? 实际工程经验证明
? 多重继承接口不会带来二义性和复杂性等问题
? 多重继承可以通过精心设计用单继承和接口来代替
接口类只是一个功能说明 , 而不是功能实现 。子类需要根据功能说明定义功能实现
接口使用示例
#include <cstdlib>
#include <iostream>
using namespace std;
class Interface1
{
public:
virtual void print() = 0;
virtual int add(int i, int j) = 0;//第一个接口 加法
};
struct Interface2
{
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;
Interface1* i1 = &c;
Interface2* i2 = &c;
//实际上调用的一个函数
cout<<i1->add(7, 8)<<endl;//调用 Interface1 --> virtual int add(int i, int j) = 0;
cout<<i2->add(7, 8)<<endl;//调用 Interface2 --> virtual int add(int i, int j) = 0;
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}