MATLAB 帮助中心
成员函数调用期间 this 指针为空
this
此项针对 this 指针解引用的检查用于确定该指针是否为 NULL。
NULL
Review and Fix Null This-pointer Calling Method Checks
全部展开
#include <stdlib.h> class Company { public: Company(int initialNumber):numberOfClients(initialNumber) {} void addNewClient() { numberOfClients++; } protected: int numberOfClients; }; void main() { Company* myCompany = NULL; myCompany->addNewClient(); }
在此示例中,指针 myCompany 被初始化为 NULL。因此,当使用该指针来调用成员函数 addNewClient 时,调用方法的 this 指针为空将生成红色错误。
myCompany
addNewClient
一种可能的更正方法是使用 new 运算符以有效的内存地址来初始化 myCompany。
new
#include <stdlib.h> class Company { public: Company(int initialNumber):numberOfClients(initialNumber) {} void addNewClient() { numberOfClients++; } protected: int numberOfClients; }; void main() { Company* myCompany = new Company(0); myCompany->addNewClient(); }
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
欧洲
亚太
联系您当地的办事处