warning C4150: 删除指向不完整“XXX”类型的指针;没有调用析构函数

简介:

 

情况源于我的之前一片博客《C++ 智能指针》,在我写demo代码的时候。

 

向前申明了class Phone, 然后再U_ptr类析构函数中delete Phone的指针。

出现warning C4150: 删除指向不完整“XXX”类型的指针;没有调用析构函数

 

 

这个waring会导致内存泄露。前向申明的类的析构函数没有被调用

 

 

出现warning的代码如下:

 

#include <iostream> using namespace std; class ConnectManager; class CommManager; class Phone; class U_ptr { friend class ConnectManager; friend class CommManager; public: U_ptr(Phone *pH): use(0), pPhone(pH) { } ~U_ptr() { delete pPhone; } private: Phone *pPhone; size_t use; }; class Phone { public: Phone() { cout << "Hello World!" << endl; } ~Phone() { cout << "Bye!" << endl; } }; class ConnectManager { public: ConnectManager(U_ptr *p): pUptr(p) { ++pUptr->use; cout << "ConnectManager: Hi I get the Phone" << endl; cout << pUptr->use << " users" << endl; } ~ConnectManager() { cout << "ConnectManaer: Can I delete the Phone?" << endl; if (--pUptr->use == 0) { cout << "Yes, You can" << endl; delete pUptr; } else { cout << "No, You can't, The Phone is in use" << endl; cout << pUptr->use << " users" << endl; } } private: U_ptr *pUptr; }; class CommManager { public: CommManager(U_ptr *p): pUptr(p) { ++pUptr->use; cout << "CommManager: Hi I get the Phone" << endl; cout << pUptr->use << " users" << endl; } ~CommManager() { cout << "CommManager: Can I delete the Phone" << endl; if (--pUptr->use == 0) { cout << "Yes, You can" << endl; } else { cout << "No, You can't. The Phone is in use" << endl; cout << pUptr->use << " users" << endl; } } private: U_ptr *pUptr; }; int main(void) { Phone *symbian = new Phone(); U_ptr *pU = new U_ptr(symbian); ConnectManager connManager = ConnectManager(pU); CommManager commManager = CommManager(pU); }  

 

 

出现原因:

 

class Phone;这种方式向前申明,其后面的类只能申明其指针,前向申明以后的类无法看到其类实体。

 

所以,delete的时候,Phone的析构函数对后面的类是透明不可见的,除非使用头文件包含。

目录
相关文章
|
29天前
|
存储 C语言
文件的类型指针
文件的类型指针
13 0
|
29天前
|
编译器 C语言
void的指针类型
void的指针类型
8 0
|
1月前
|
存储 程序员 C++
在C++编程语言中指针的作用类型
在C++编程语言中指针的作用类型
14 0
|
27天前
|
C语言
Void 指针类型
Void 指针类型
8 0
|
1月前
|
存储 C语言
C语言指针类型和空类型详解
C语言指针类型和空类型详解
21 0
|
1月前
|
存储 C++
在C++语言中函数指针的作用类型
在C++语言中函数指针的作用类型
10 0
|
2月前
|
程序员 编译器 C++
【C++11】 指针空类型 - nullptr
【C++11】 指针空类型 - nullptr
19 0
|
2月前
|
编译器 C语言 C++
C++类和对象的细节原理:this指针、构造函数和析构函数、深浅拷贝、运算符重载、初始化列表、类的各种成员和方法
C++类和对象的细节原理:this指针、构造函数和析构函数、深浅拷贝、运算符重载、初始化列表、类的各种成员和方法
34 0
|
3月前
|
编译器
void * 类型指针
void * 类型指针
21 1
|
3月前
|
存储 C语言
learn_C_deep_6 (布尔类型、布尔与“零值“、浮点型与“零值“、指针与“零值“的比较)(下)
learn_C_deep_6 (布尔类型、布尔与“零值“、浮点型与“零值“、指针与“零值“的比较)