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

How to pass a class as an argument to another classes constructor?

本文关键字:参数传递 另一个 构造函数 一个      更新时间:2023-10-16

我得到的问题出在构造函数中的 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;
}

人.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();
}

生日.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;
}

人:

#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

生日.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

问题

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

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

在没有成员初始值设定项列表中初始化成员的情况下,就像在问题中所做的那样

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

使用Birthday自动生成的复制构造函数,将使用默认构造函数。Birthday没有默认构造函数(如果您有用户定义的构造函数,编译器将不会创建默认构造函数),因此编译器会发出错误并生成任何可用对象。

选项(除了,比如,放弃愤怒或其他什么)

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

旁注:您可以避免多余的Birthday对象徘徊。 如果按照最新的C++标准构建,您可以

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

如果您没有至少 C++11 支持,您可以

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

这将创建一个临时Birthday,一直活到线路的尽头。一个好的编译器知道优化蓝精灵的技巧,所以你可能永远不知道临时的。

路过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这样的简单类,它还不错,但与选项一相比没有优势,并且使编译器更难应用几个有用的优化。对于复杂或难以构造的类,此选项将遍历默认构造的工作,然后添加赋值所需的任何工作。

旁注:如果现代C++可用,则可以轻松使用简单的默认构造函数。加

Birthday() = default;

到类定义。

它所需要的只是一个默认的生日构造函数。