C++调用无参构造函数


之前不知道为什么调用无参构造函数不能加(),是因为加上了()这样的形式就和函数声明语句一样了

#include<bits/stdc++.h>
using namespace std; const int maxn=1e5+5; class people{ public: people(){ printf("123\n"); } }; class student :public people{ public: int x=1; //调用下面这个构造函数,如果不传参数会和 student()重复,如果传一个参数会和 student(int x)重复 // student(int x=2,int y=1){ // printf("111\n"); // }
        student(int x){ printf("222\n"); } student(){ printf("5555\n"); } }; int main() { //下面这样写不是调用类的无参构造函数,而是一个函数的声明(可不是创建一个student类型的对象)。所以调用无参构造函数不要加() //student stu();
 student stu; printf("%d\n",stu.x); return 0; }

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM