java - 这两个 new 的含义分别是什么?
黄舟
黄舟 2017-04-18 09:37:14
0
3
424
黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(3)
左手右手慢动作

C++: As promisednew

#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;
}
Of course this programming habit is very bad

And C++

is not new as simple as you think

Ty80

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

大家讲道理

Your c++ doesn’t use new

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template