标签:malloc not pre 阶段 info 引用 直接 面向对象 .cpp
new关键字与malloc函数的区别:
下面的代码输出什么?为什么?
示例——new和malloc的区别:
#include <iostream>
#include <cstdlib>
using namespace std;
class Test
{
public:
Test()
{
cout << "Test::Test()" << endl;
}
};
int main()
{
Test* pn = new Test;
Test* pm = (Test*)malloc(sizeof(Test));
return 0;
}
运行结果为:
[root@bogon Desktop]# g++ test.cpp
[root@bogon Desktop]# ./a.out
Test::Test()
new和malloc的区别:
下面的代码输出什么?为什么?
示例——delete和free的区别:
#include <iostream>
#include <cstdlib>
using namespace std;
class Test
{
int* mp;
public:
Test()
{
cout << "Test::Test()" << endl;
mp = new int(100);
cout << *mp << endl;
}
~Test()
{
delete mp;
cout << "~Test::Test()" << endl;
}
};
int main()
{
Test* pn = new Test;
Test* pm = (Test*)malloc(sizeof(Test));
free(pn);
free(pm);
return 0;
}
运行结果为:
[root@bogon Desktop]# g++ test.cpp
[root@bogon Desktop]# ./a.out
Test::Test()
100
可以看到,free不会触发析构函数,会造成内存泄漏!
delete和free的区别:
构造函数是否可以成为虚函数?析构函数是否可以成为虚函数?
构造函数不可能成为虚函数:
析构函数可以成为虚函数:
示例——不把析构声明为虚函数:
#include <iostream>
using namespace std;
class Base
{
public:
Base()
{
cout << "Base()" << endl;
}
~Base()
{
cout << "~Base()" << endl;
}
};
class Derived : public Base
{
public:
Derived()
{
cout << "Derived()" << endl;
}
~Derived()
{
cout << "~Derived()" << endl;
}
};
int main()
{
Base* p = new Derived();
cout << endl;
delete p;
return 0;
}
运行结果为:
[root@bogon Desktop]# g++ test.cpp
[root@bogon Desktop]# ./a.out
Base()
Derived()
~Base()
由于没有把析构声明为虚函数,因此,编译器直接根据指针p的类型来决定调用哪一个析构函数,又由于指针p的类型是父类的,所以编译器认为直接调用父类的析构函数就可以了。
示例——把析构声明为虚函数:
#include <iostream>
using namespace std;
class Base
{
public:
Base()
{
cout << "Base()" << endl;
}
virtual ~Base()
{
cout << "~Base()" << endl;
}
};
class Derived : public Base
{
public:
Derived()
{
cout << "Derived()" << endl;
}
~Derived()
{
cout << "~Derived()" << endl;
}
};
int main()
{
Base* p = new Derived();
cout << endl;
delete p;
return 0;
}
运行结果为:
[root@bogon Desktop]# g++ test.cpp
[root@bogon Desktop]# ./a.out
Base()
Derived()
~Derived()
~Base()
如果将构造函数声明为虚函数,报错信息如下:
error: constructors cannot be declared virtual
构造函数中是否可以发生多态?析构函数中是否可以发生多态?
构造函数中不可能发生多态行为:
析构函数中不可能发生多态行为:
构造函数和析构函数中不能发生多态行为,只调用当前类中定义的函数版本!!
示例——构造函数和析构函数中不可能发生多态:
#include <iostream>
using namespace std;
class Base
{
public:
Base()
{
cout << "Base()" << endl;
func();
}
virtual void func()
{
cout << "Base::func()" << endl;
}
virtual ~Base()
{
func();
cout << "~Base()" << endl;
}
};
class Derived : public Base
{
public:
Derived()
{
cout << "Derived()" << endl;
func();
}
virtual void func()
{
cout << "Derived::func()" << endl;
}
~Derived()
{
func();
cout << "~Derived()" << endl;
}
};
int main()
{
Base* p = new Derived();
cout << endl;
delete p;
return 0;
}
运行结果为:
[root@bogon Desktop]# g++ test.cpp
[root@bogon Desktop]# ./a.out
Base()
Base::func()
Derived()
Derived::func()
Derived::func()
~Derived()
Base::func()
~Base()
继承中如何正确的使用强制类型转换?
dynamic_cast是与继承相关的类型转换关键字
dynamic_cast要求相关的类中必须有虚函数
用于有直接或者间接继承关系的指针(引用)之间
编译器会检查dynamic_cast的使用是否正确
类型转换的结果只可能在运行阶段才能得到
示例——dynamic_cast的使用:
#include <iostream>
using namespace std;
class Base
{
public:
Base()
{
cout << "Base::Base()" << endl;
}
virtual ~Base()
{
cout << "Base::~Base()" << endl;
}
};
class Derived : public Base
{
};
int main()
{
Base* p = new Base;
Derived* pd = dynamic_cast<Derived*>(p);
if( pd != NULL )
{
cout << "pd = " << pd << endl;
}
else
{
cout << "Cast error!" << endl;
}
delete p;
return 0;
}
运行结果为:
[root@bogon Desktop]# g++ test.cpp
[root@bogon Desktop]# ./a.out
Base::Base()
Cast error!
Base::~Base()
C++解析(25):关于动态内存分配、虚函数和继承中强制类型转换的疑问
标签:malloc not pre 阶段 info 引用 直接 面向对象 .cpp
原文地址:https://www.cnblogs.com/PyLearn/p/10092188.html