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.
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’
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’