首先,给出基类animal和子类fish
- //==============================================================
- // animal.h
- //
- // begin : 2012-06-30
- // author : zwq
- // describe: 非虚函数情况下,将子类指针赋给积累指针,验证最终调用
- // 基类函数还是子类函数。
- //==============================================================
- #ifndef ANIMAL_H
- #define ANIMAL_H
- //===============================================================
- //
- // animal
- // 动物基类
- //
- //===============================================================
- class animal
- {
- public:
- void breathe(); // 非虚函数
- };
- //===============================================================
- //
- // animal
- // 鱼类,集成于动物基类
- //
- //===============================================================
- class fish : public animal
- {
- public:
- void breathe(); // 非虚函数
- };
- #endif
//==============================================================
// animal.h
//
// begin : 2012-06-30
// author : zwq
// describe: 非虚函数情况下,将子类指针赋给积累指针,验证最终调用
// 基类函数还是子类函数。
//==============================================================
#ifndef ANIMAL_H
#define ANIMAL_H
//===============================================================
//
// animal
// 动物基类
//
//===============================================================
class animal
{
public:
void breathe(); // 非虚函数
};
//===============================================================
//
// animal
// 鱼类,集成于动物基类
//
//===============================================================
class fish : public animal
{
public:
void breathe(); // 非虚函数
};
#endif
- #include "StdAfx.h"
- #include <iostream>
- #include "Animal.h"
- using namespace std;
- //===============================================================
- //
- // animal
- // 动物基类
- //
- //===============================================================
- void animal::breathe()
- {
- cout << "animal breathe" << endl;
- }
- //===============================================================
- //
- // animal
- // 鱼类,集成于动物基类
- //
- //===============================================================
- void fish::breathe()
- {
- cout << "fish bubble" << endl;
- }
#include "StdAfx.h"
#include <iostream>
#include "Animal.h"
using namespace std;
//===============================================================
//
// animal
// 动物基类
//
//===============================================================
void animal::breathe()
{
cout << "animal breathe" << endl;
}
//===============================================================
//
// animal
// 鱼类,集成于动物基类
//
//===============================================================
void fish::breathe()
{
cout << "fish bubble" << endl;
}
一.基类指针和子类指针之间相互赋值
(1)将子类指针赋值给基类指针时,不需要进行强制类型转换,C++编译器将自动进行类型转换。因为子类对象也是一个基类对象。
(2)将基类指针赋值给子类指针时,需要进行强制类型转换,C++编译器将不自动进行类型转换。因为基类对象不是一个子类对象。子类对象的自增部分是基类不具有的。
执行以下代码,看看会报什么错误:
- int main(int argc, char* argv[])
- {
- ExamAnimal();
- return 0;
- }
- void ExamAnimal()
- {
- // 将子类指针直接赋给基类指针,不需要强制转换,C++编译器自动进行类型转换
- // 因为fish对象也是一个animal对象
- animal* pAn;
- fish* pfh = new fish;
- pAn = pfh;
- delete pfh;
- pfh = NULL;
- // 将基类指针直接赋给子类指针,需要强制转换,C++编译器不会自动进行类型转换
- // 因为animal对象不是一个fish对象
- fish* fh1;
- animal* an1 = new animal;
- // 没有进行强制类型转化
- fh1 = an1;
- delete an1;
- an1 = NULL;
- }
int main(int argc, char* argv[])
{
ExamAnimal();
return 0;
}
void ExamAnimal()
{
// 将子类指针直接赋给基类指针,不需要强制转换,C++编译器自动进行类型转换
// 因为fish对象也是一个animal对象
animal* pAn;
fish* pfh = new fish;
pAn = pfh;
delete pfh;
pfh = NULL;
// 将基类指针直接赋给子类指针,需要强制转换,C++编译器不会自动进行类型转换
// 因为animal对象不是一个fish对象
fish* fh1;
animal* an1 = new animal;
// 没有进行强制类型转化
fh1 = an1;
delete an1;
an1 = NULL;
}
编译时,报如下错误信息:
--------------------Configuration: CPlusPlusPrimer - Win32 Debug--------------------
Compiling...
CPlusPlusPrimer.cpp
E:\Study\example\CPlusPlusPrimer\CPlusPlusPrimer.cpp(94) : error C2440: '=' : cannot convert from 'class animal *' to 'class fish *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.
CPlusPlusPrimer.exe - 1 error(s), 0 warning(s)
根据以上错题提示信息,对代码做如下修改:
- void ExamAnimal()
- {
- // 将子类指针直接赋给基类指针,不需要强制转换,C++编译器自动进行类型转换
- // 因为fish对象也是一个animal对象
- animal* pAn;
- fish* pfh = new fish;
- pAn = pfh;
- delete pfh;
- pfh = NULL;
- // 将基类指针直接赋给子类指针,需要强制转换,C++编译器不会自动进行类型转换
- // 因为animal对象不是一个fish对象
- fish* fh1;
- animal* an1 = new animal;
- // 修改处:
- // 进行强制类型转化
- fh1 = (fish*)an1;
- delete an1;
- an1 = NULL;
- }
void ExamAnimal()
{
// 将子类指针直接赋给基类指针,不需要强制转换,C++编译器自动进行类型转换
// 因为fish对象也是一个animal对象
animal* pAn;
fish* pfh = new fish;
pAn = pfh;
delete pfh;
pfh = NULL;
// 将基类指针直接赋给子类指针,需要强制转换,C++编译器不会自动进行类型转换
// 因为animal对象不是一个fish对象
fish* fh1;
animal* an1 = new animal;
// 修改处:
// 进行强制类型转化
fh1 = (fish*)an1;
delete an1;
an1 = NULL;
}
再次编译,通过。
二.子类指针赋给基类指针时内存分析
(1)int变量赋给char变量
整型int转换为char类型时,只有一个字节的内容能够放进char类型,剩下的三个字节内容放不下,被截掉,丢失精度。
两个变量或者对象进行转换时,一定要看两者的内存模型是否互相匹配。
(2)子类fish指针赋给基类animal指针
下面看看子类fish指针赋给基类animal指针时,内存的变化:
当我们构造fish类的对象时,首先要调用animal类的构造函数去构造animal类的构造函数,然后才调用fish类的构造函数完成自身部分的构造,从而拼接出一个完整的fish对象。当我们将fish类对象转换为animal类对象时,该对象就被认为是原对象整个内存模型的上半部分,也就是图中animal对象的内存部分。当我们利用类型转换后的对象指针去调用它的方法时,自然是调用它所在的内存中的方法。
在这里,animal类对象类似于char类型的对象,fish类对象类似于int类型的对象,将fish类对象赋给animal类对象时,会截取fish类对象自身的部分,剩下fish类对象中的animal部分。
(2)基类animal指针赋给子类fish指针
基类animal对象包含的信息少,类fish对象包含的信息多,将信息少的对象直接转换为信息多的对象时(没有强制类型转换),显然是无法构造出多出的信息。在编译时,也会发生如下错误:error C2440: '=' : cannot convert from 'class animal *' to 'class fish *'。
这时,需要做强制类型转换:
- // 将基类指针直接赋给子类指针,需要强制转换,C++编译器不会自动进行类型转换
- // 因为animal对象不是一个fish对象
- fish* fh1;
- animal* an1 = new animal;
- // 进行强制类型转化
- fh1 = (fish*)an1;