小编Gla*_*r11的帖子

在c ++中返回*这是安全的吗?

我想知道从函数返回*this是否安全.这个问题显示了一些你可以做到的方法,我的问题是这个例子:

struct test {
    string t;
    string b;
public:
    test& A(string test)       { this->t=test; return *this; }

    test& B(string test)       { this->b=test; return *this; }
};

int main() {

    auto a = test().A("a").B("b").A("new a");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

是否会出现内存泄漏?

c++ pointers memory-leaks

4
推荐指数
1
解决办法
119
查看次数

标签 统计

c++ ×1

memory-leaks ×1

pointers ×1