未调用c++OOP的默认构造函数
Default constructor not being called c++ OOP
所以我正在用c++制作一个处理向量的程序,它大部分都在那里,但我只是想测试它,所以我有了这个:
class vector3 {
protected:
double x,y,z;
public:
// Default 3 vector Constructor
vector3() {
cout << "Default constructor called." << endl;
x=y=z=0;
}
vector3(double xin, double yin, double zin) {
cout << "parametrised constructor called." << endl;
x=xin;
y=yin;
z=zin;
}
};
(还有更多的东西,<<
的东西等等(
作为main()
,我有:
int main() {
vector3 vec1();
cout << "Vector 1: " << vec1 << endl;
vector3 vec2(0, 0, 0);
cout << "Vector 2: " << vec2 << endl;
return 0;
}
它给出了输出:
Vector 1: 1
Parametrised constructor called.
Vector 2: (0,0,0)
Destroying 3 vector
但他们不应该给出相同的输出吗?我是不是错过了一些显而易见的东西?
编辑:编译时有一条警告说:
test.cpp: In function ‘int main()’:
test.cpp:233:26: warning: the address of ‘vector3 vec1()’ will always evaluate as ‘true’ [-Waddress]
cout << "Vector 1: " << vec1 << endl;
vector3 vec1();
您在这里声明一个函数,并显示一个函数指针。用途:
vector3 vec1;
相关文章:
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 初始化具有非默认构造函数的std::数组项的更好方法
- 具有默认模板类型的默认构造函数的类型推导
- 如何使用非默认构造函数实例化模板化类
- 有没有一种代码密度较低的方法来使用非默认构造函数初始化数组?
- 声明没有默认构造函数的字段
- 没有默认构造函数作为模板参数的自定义比较器
- C++17 没有默认构造函数的地图放置(私有默认构造函数)
- 使用移动调用对等构造函数unique_ptr默认构造函数
- C++复制构造函数和默认构造函数
- 将向量从 N1 缩小到 N2 项,而不触发默认构造函数并仅使用 move 语义
- 为什么即使我调用参数化构造函数也会调用默认构造函数?
- 具有非默认构造函数的单例类
- 在 C++ 中声明 const 对象需要用户定义的默认构造函数.如果我有一个可变成员变量,为什么不呢?
- 如何处理没有默认构造函数但在另一个构造函数中构造的对象?
- 在C++中使用默认构造函数初始化对象的不同方法
- 在没有默认构造函数的情况下创建的派生对象
- 强制使用默认构造函数对成员进行未初始化的声明
- 使用默认构造函数初始化对象的不同方法
- 创建类类型的动态分配数组,其中类不得具有默认构造函数