C++ 构造函数
-
构造函数
1. 定义
1. constructors are special functions with no return type
2. automatically called when an object is created
3. after the object has been constructed, they are no longer used
2. 性质
1. the function name must be the same as the struct name
2. constructors can be overload, i.e. there can be multiple constructors with different parameter
3. 例子
struct Rectangle
{
int m_width, m_height;
//构造函数
//initialize member m_width, member m_height;
Rectangle(int x, int y): m_width(x), m_height(y) { }
//普通函数
int area() { return m_width * m_height; }
};
int main()
{
Rectangle rect(3, 4); // constructor called at object creation
cout << "area: " << rect.area() << endl;
return 0;
}
4. 易错点
height and width are not initialized in the same order in which they are declared`
struct Rectangle
{
int m_width, m_height;
//构造函数
//作用:initialize member m_width, member m_height;
Rectangle(int x, int y): m_height(x), m_width(y) { }
//普通函数
int area() { return m_width * m_height; }
};
constructors cannot have a return type
struct Rectangle
{
int m_width, m_height;
//构造函数
//initialize member m_width, member m_height;
Void Rectangle(int x, int y): m_height(x), m_width(y) { }
//普通函数
int area() { return m_width * m_height; }
};
a constructor must have the same name as the struct
struct Rectangle
{
int m_width, m_height;
//构造函数
//initialize member m_width, member m_height;
init (int x, int y): m_height(x), m_width(y) { }
//普通函数
int area() { return m_width * m_height; }
};
5. initializer list 的作用
they are used for to call the constructors of data members passing them arguments. These cannot be called from the function body.
struct A
{
int m_x;
A(): m_x(0) {} // constructor with no argument
A(int x): m_x(x) {} // constructor with one argument
};
struct B
{
A m_a;
// from the initializer list we can use the constructor of A
B (int x): m_a(x)
{// inside the body we cannot use constructor of }
}
int main()
{
A a(1);
B b(1);
return 0;
}
Can we omit the initializer list?
Yes, but the constructor with no arguments(default constructor) will be called anyway.
struct A
{
int m_x;
A(): m_x(3) {} // constructor with no argument
A(int x): m_x(x) {} // constructor with one argument
};
struct B
{
A m_a; // from the initializer list we can use the constructor of A
// no initializer list, hence the default constructor of A is called
// which sets m_x = 3;
B (int x)
{
// inside the body we cannot use constructor of A
// so modify m_x directly
m_a.m_x = x;
}
}
int main()
{
A a(1);
B b(1);
return 0;
}
6. Copy and Default Constructor
- default constructor: has an empty parameter list. If no other costructor is explicity defined, implicitly added by the compiler.
- copy constructor: has just one parameter, a const reference of the same type as the object itself
copy constructor : Normally we implement the copy constructor only if we want to do something different from the default copy behaviour
struct Position
{
double m_x, m_y;
// copy constructor
// copies p.m_x to m_x, p.m_y to m_y
Position(const Position& p): m_x(p.m_x), m_y(p.m_y) {}
// normal constructor
Position(double x, double y): m_x(x), m_y(y) {}
};
default constructor
struct Position
{
double m_x, m_y;
Position(): m_x(0), m_y(0) {}
// normal constructor
Position(double x, double y): m_x(x), m_y(y) {}
};
7. Constructor Example
void print (const char *name, const Position& p)
{
cout << name << "(" << p.m_x << "," << p.m_y << ")" << endl;
}
int main()
{
Position x; // call default constructor
Position t(); // call default constructor
Position y(1,2); // call constructor Position(int, int)
Position z(y); // call copy constructor
print("x", x);
print("t", t);
print("y", y);
print("z", z);
}
最好能够给出对应的C版本,C11,14以后其实区别挺大的