注册 登录
编程论坛 C++教室

类内初始化成员问题

xiaodu000 发布于 2014-06-03 17:43, 620 次点击
#include<iostream>
using namespace std;
class Student{
    private:
        string s[3];
    public:
        Student()
        {
            cout<<"use one"<<endl;
        }
        Student(string a,string b,string c)
        {
            s[0] = a;
            s[1] = b;
            s[2] = c;
            cout<<"use two"<<endl;
        }
        
};
int main()
{
    Student s1("123","1234","12345");
    Student s2();
    return 0;
}
只有本站会员才能查看附件,请 登录

帮忙看一下,为什么会跳过对象s2直接结束。
6 回复
#2
xiaodu0002014-06-03 17:52
好吧,我只是想探究一下Student s2和Student s2()的不同。
#3
TonyDeng2014-06-04 02:30
Student s2()多了個圓括號。

編譯時就警告你
warning C4930: “Student s2(void)”: 未调用原型函数(是否是有意用变量定义的?)
沒看到?

[ 本帖最后由 TonyDeng 于 2014-6-4 02:32 编辑 ]
#4
gobraves2014-06-04 14:44
没跳过,跳过了怎么可能还输出use two
#5
gobraves2014-06-04 14:45
一个是带参的构造函数,一个是无参的构造函数
#6
hubinyes2014-06-06 21:19
#7
律吕调阳2014-06-09 14:05
Student s2();
这样定义对象会产生误解,可能是一个函数原型:
返回类型为MyClass,不需要参数的函数的原型
去掉括号就行了
1