警告:derived class 的成员变量在 base class 之后初始化
warning: derived class's member variable is initialized after base class
题目简单明了。这是代码:
class xxx : public Scheduled
{
long int _wait_time;
boost::function< void() > _cb;
mutable boost::mutex _mutex;
public:
xxx(boost::function< void() > callback, unsigned int wait_time = 10000000)
:_wait_time(wait_time),_cb(callback)
,Scheduled(Timer(_wait_time, 0))
{
this->stop();
}
};
虽然我尊重初始化顺序,但这是我收到的警告:
---: In constructor ‘xxx::xxx(boost::function<void ()()>, unsigned int)’:
---: warning: ‘xxx::_cb’ will be initialized after
---: warning: base ‘Scheduled’
---: warning: when initialized here
有什么想法吗?谢谢
基础 class 构造函数必须是初始化列表中的第一个:
xxx(boost::function< void() > callback, unsigned int wait_time = 10000000)
:Scheduled(Timer(_wait_time, 0))
,_wait_time(wait_time),
,_cb(callback)
{
this->stop();
}
您正在初始化派生 class 成员,然后再构造基础 class。
xxx(boost::function< void() > callback, unsigned int wait_time = 10000000)
:_wait_time(wait_time),_cb(callback) <- derived class memebers
,Scheduled(Timer(_wait_time, 0)) <- Base class
{
this->stop();
}
在 C++ 中,基 class 必须在派生 class 的成员初始化之前构造。所以警告告诉你,即使你在派生成员之后有基 class 初始化,它也会首先构造基 class。
如果您将代码更改为
xxx(boost::function< void() > callback, unsigned int wait_time = 10000000)
:Scheduled(Timer(wait_time, 0))
^ use wait_time instead of _wait_time
,_wait_time(wait_time),
,_cb(callback)
{
this->stop();
}
您应该不会再收到警告。
添加到其他答案:
如果您的初始化程序的顺序与它们在 class.
中出现的顺序不同,则可能会出现此警告的另一个原因
示例:
struct A { .. };
struct B { .. };
struct C
{
A a;
B b;
C(..) : a(..), b(..) {} // OK
C(..) : b(..), a(..) {} // Wreorder-warning
};
题目简单明了。这是代码:
class xxx : public Scheduled
{
long int _wait_time;
boost::function< void() > _cb;
mutable boost::mutex _mutex;
public:
xxx(boost::function< void() > callback, unsigned int wait_time = 10000000)
:_wait_time(wait_time),_cb(callback)
,Scheduled(Timer(_wait_time, 0))
{
this->stop();
}
};
虽然我尊重初始化顺序,但这是我收到的警告:
---: In constructor ‘xxx::xxx(boost::function<void ()()>, unsigned int)’:
---: warning: ‘xxx::_cb’ will be initialized after
---: warning: base ‘Scheduled’
---: warning: when initialized here
有什么想法吗?谢谢
基础 class 构造函数必须是初始化列表中的第一个:
xxx(boost::function< void() > callback, unsigned int wait_time = 10000000)
:Scheduled(Timer(_wait_time, 0))
,_wait_time(wait_time),
,_cb(callback)
{
this->stop();
}
您正在初始化派生 class 成员,然后再构造基础 class。
xxx(boost::function< void() > callback, unsigned int wait_time = 10000000)
:_wait_time(wait_time),_cb(callback) <- derived class memebers
,Scheduled(Timer(_wait_time, 0)) <- Base class
{
this->stop();
}
在 C++ 中,基 class 必须在派生 class 的成员初始化之前构造。所以警告告诉你,即使你在派生成员之后有基 class 初始化,它也会首先构造基 class。
如果您将代码更改为
xxx(boost::function< void() > callback, unsigned int wait_time = 10000000)
:Scheduled(Timer(wait_time, 0))
^ use wait_time instead of _wait_time
,_wait_time(wait_time),
,_cb(callback)
{
this->stop();
}
您应该不会再收到警告。
添加到其他答案:
如果您的初始化程序的顺序与它们在 class.
示例:
struct A { .. };
struct B { .. };
struct C
{
A a;
B b;
C(..) : a(..), b(..) {} // OK
C(..) : b(..), a(..) {} // Wreorder-warning
};