【发布时间】:2019-01-06 13:56:26
【问题描述】:
我对@987654321@ 提出的完全相同的问题感兴趣, 但对于 C++。有没有办法隐式传递参数 基类构造函数?这是我尝试过的一个小例子 这是行不通的。当我移除 cmets 并 调用基地时 类构造函数显式,一切正常。
struct Time { int day; int month; };
class Base {
public:
Time time;
Base(Time *in_time)
{
time.day = in_time->day;
time.month = in_time->month;
}
};
class Derived : public Base {
public:
int hour;
// Derived(Time *t) : Base(t) {}
};
int main(int argc, char **argv)
{
Time t = {30,7};
Derived d(&t);
return 0;
}
如果有帮助,这里是完整的编译行 + 编译错误:
$ g++ -o main main.cpp
main.cpp: In function ‘int main(int, char**)’:
main.cpp:19:14: error: no matching function for call to ‘Derived::Derived(Time*)’
Derived d(&t);
^
【问题讨论】:
标签: c++ inheritance constructor base-class