关于C++结构体的构造函数
在网上看到这句话:结构不能包含显式的无参数构造函数。结构成员将自动初始化为它们的默认值。不太明白,就用实例试了下:
#include <iostream>
struct point
{
public:
point()
{
std::cout<<"default constructor\n";
}
point(double x,double y):x_(x),y_(y)
{
std::cout<<"constructor("<<x<<", "<<y<<")\n";
}
~point()
{
std::cout<<"default destructor\n";
}
double get_x()
{
return x_;
}
double get_y()
{
return y_;
}
private:
double x_;
double y_;
};
int main()
{
point pt;
std::cout<<pt.get_x()<<"\n";
std::cout<<pt.get_y()<<"\n";
}
没有报错,输出正常,求人解释那就话到底是什么意思呢?
页:
[1]