人生最曼妙的风景,竟是内心的淡定与从容!
C++: As promisednew
new
#include<iostream> class test { public: test() = default; test(int b) { a = b; } void A() { std::cout << a << std::endl; } private: int a; }; int main(int argc,char *argv[]) { test(1).A(); //没问题 (*(new test(1))).A(); //内存泄露 (new test(1))->A(); //上一行的语法糖 return 0; }
is not new as simple as you think
Your abovejava/c++构造对象的方法是有区别的,还有c++才是返回一个真正的对象,而java是类似于指针的东西,你恰好理解反了,而且c++也有类似于java返回指针的用法:new test(1)这样返回的就是一个指针~~还有就是 这样不行?test(1).A();(new test(1))->A(); // Of course this is not recommended because there is a memory leak
java/c++
c++
java
new test(1)
test(1).A()
(new test(1))->A();
Your c++ doesn’t use new
C++: As promised
Of course this programming habit is very badnew
is not
new
as simple as you thinkYour above
java/c++
构造对象的方法是有区别的,还有c++
才是返回一个真正的对象,而java
是类似于指针的东西,你恰好理解反了,而且c++
也有类似于java
返回指针的用法:new test(1)
这样返回的就是一个指针~~还有就是 这样不行?
test(1).A()
;(new test(1))->A();
// Of course this is not recommended because there is a memory leakYour c++ doesn’t use new