小编use*_*451的帖子

构造函数中的初始化列表

我听说在构造函数中使用初始化列表的优点是不会有类型对象的额外副本.但是对于T类构造函数中的以下代码,它意味着什么?如果我评论分配并使用初始化列表会有什么区别?

#include <iostream>
using std::cout;
using std::endl;
using std::ostream;

class X {

public:

    X(float f_x = 0, float f_y = 0):x(f_x), y(f_y) {}


    ~X() {}

    X(const X& obj):x(obj.x), y(obj.y) {}

    friend ostream& operator << (ostream &os, X &obj);

private:
    float x;
    float y;
};

ostream& operator << (ostream &os, X &obj)
{ os << "x = " << obj.x << " y = " << obj.y; return os;}

class T {

public:

    T(X &obj) : x(obj) { /* x = …
Run Code Online (Sandbox Code Playgroud)

c++ constructor initialization-list

5
推荐指数
2
解决办法
1362
查看次数

标签 统计

c++ ×1

constructor ×1

initialization-list ×1