如何将一个类作为参数传递给另一个类构造函数?

时间:2018-05-16 19:05:23

标签: c++

我得到的问题是在构造函数中的Person.cpp中。当我这样做时,一切正常

Person::Person(string n,Birthday b)
:    name(n) ,
    birthday(b)
{}

当我这样做时,我得到错误......

Person::Person(string n,Birthday b) {
    name = n;
    birthday = b;
}

我的问题是如何将一个类作为参数传递给另一个类构造函数?

主:

#include <iostream>
#include "Birthday.h"
#include "Person.h"

using namespace std;

int main()
{
    Birthday birth(13,4,1996);

    Person bill("Billy Par",birth);
    bill.printInfo();
    return 0;
}

Person.cpp:

#include "Person.h"
#include <iostream>

using namespace std;

Person::Person(string n,Birthday b)
:    name(n) ,
    birthday(b)
{}

void Person::printInfo() {
    cout << name << " was born on ";
    birthday.printDate();
}

Birthday.cpp:

#include "Birthday.h"
#include <iostream>

using namespace std;

Birthday::Birthday(int d,int m,int y) {
    day = d;
    month = m;
    year = y;
}

void Birthday::printDate() {
    cout << day << "/" << month << "/" << year << endl;
}

Person.h:

#ifndef PERSON_H
#define PERSON_H

#include <string>
#include "Birthday.h"

using namespace std;

class Person {
public:
    Person(string n,Birthday b);
    void printInfo();
private:
    string name;
    Birthday birthday;
};

#endif // PERSON_H

Birthday.h:

#ifndef BIRTHDAY_H
#define BIRTHDAY_H

class Birthday {
public:
    Birthday(int d,int m,int y);
    void printDate();
private:
    int day;
    int month;
    int year;
};

#endif // BIRTHDAY_H

2 个答案:

答案 0 :(得分:0)

所有它需要工作,是一个默认的生日构造函数。

答案 1 :(得分:0)

问题

当您输入构造函数的主体时,必须已经构造了所有成员对象。

Person::Person(string n,Birthday b) 
{ // birthday must be constructed before here
    name = n;
    birthday = b; // this is an assignment, not an initialization
}

如果没有成员在member initializer list中初始化,就像在问题中所做的那样

Person::Person(string n,Birthday b)
:    name(n) ,
    birthday(b)
{}

使用Birthday&#39; s automatically-generated copy constructor,将使用默认构造函数。 Birthday没有默认构造函数(如果您有用户定义的构造函数,编译器将不会创建默认构造函数),因此编译器会发出错误并且不会生成任何可用对象。

选项(除了愤怒退出等)

1)传递Birthday并按上述方式复制。

附注:您可以避免闲置多余的Birthday对象。 如果构建到最近的C ++标准,你可以

Person bill("Billy Par", {13,4,1996});

如果你不具备至少C ++ 11的支持,你可以

Person bill("Billy Par",Birthday(13,4,1996));

这会创建一个临时的Birthday,它会一直存在。一个好的编译器知道如何优化smurf,所以你甚至可能根本不知道临时存在。

通过const参考

传递
Person::Person(string n,const Birthday &b)
:    name(n) ,
    birthday(b)
{}
通过删除副本,

有可能稍好一点,但现代编译器也知道这个技巧。

2)向Person的构造函数添加其他参数,并使用它们在成员初始化列表中调用Birthday的构造函数。

Person::Person(string n, int d, int m, int y)
:    name(n) ,
    birthday(d,m,y)
{}

个人意见,我发现

Person bill("Billy Par",Birthday(13,4,1996));

更好地表达意图并且更加多样化。

3)向Birthday

添加默认构造函数

我不喜欢这个选项。对于像Birthday这样的简单类,它不错,但没有优于选项一,并且使编译器更难以应用几个有用的优化。对于复杂或难以构造的类,此选项将执行默认构造的工作,然后添加分配所需的任何工作。

附注:如果Modern C ++可用,则一个简单的默认构造函数很容易。添加

Birthday() = default;

到类定义。