如何将一个类作为参数传递给另一个类构造函数?
How to pass a class as an argument to another classes constructor?
我得到的问题出在构造函数中的 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;
到类定义。
它所需要的只是一个默认的生日构造函数。
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 修改函数中的指针(将另一个指针作为参数传递)
- 如何将一个类的函数作为另一个类的另一个函数的参数传递
- C++重载函数,一个采用基类的参数,另一个采用派生类的参数
- 如果模板参数是另一个模板的实例化,则键入特征测试
- 将可变参数从一个函数传递到另一个函数
- 将一个宏传递到另一个宏而不是直接传递内容时会出现不需要的额外"空"参数
- 如何调用参数由另一个函数生成的函数?
- 如何在 VC++ 中通过引用传递另一个对象的方法(错误 C2664)
- 将函数上调整到基类参数的另一个
- 可以将算术表达式作为参数传递给一个函数来描述其中的逻辑
- 传递参数作为另一个函数的参数函数
- C libcurl-使用上一个卷曲的响应作为参数到另一个卷曲的参数
- 将模板变化函数及其参数传递到一个函数
- C++传递另一个方法作为参数
- C++:为默认方法参数调用另一个方法
- 如何让每个线程传递另一个参数
- C++:将部分专用模板的类型参数作为另一个模板类的成员类型
- 将多个参数从另一个类传递给线程函数
- 为什么 GCC 不允许我将模板参数用于另一个模板的参数?