主动调用类的构造函数怎么释放

C++语言 码拜 7年前 (2017-05-02) 2053次浏览
class A{
int  m_num;
    public:
       A();
       ~A();
}
int main(){
    A::A();
return 0;
}

主动调用A类的构造函数,是只调用A()函数还是开辟一个A类的对象空间?假如是开辟了空间没有对象名怎么释放内存,假如是只调用了A(),A()内部有new()函数又该怎么释放?

解决方案

10

你不能这样调用构造函数
Such a constructor name shall be used only in the declarator-id of a declaration that names a constructor or in a using-declaration.

30

$ g++ –version
g++ (Ubuntu 4.9.2-10ubuntu13) 4.9.2
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ g++ –std=c++11 –pedantic test.cpp
test.cpp: In function ‘int main()’:
test.cpp:16:10: error: cannot call constructor ‘A::A’ directly [-fpermissive]
A::A();
^
test.cpp:16:10: note: for a function-style cast, remove the redundant ‘::A’

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明主动调用类的构造函数怎么释放
喜欢 (0)
[1034331897@qq.com]
分享 (0)