【发布时间】:2014-06-23 00:02:38
【问题描述】:
我正在从结构化 C 迁移到 OOP C++,并且在 C++ 中声明/定义构造函数时,我经常发现“:”符号作为运算符的特殊用途。我大致了解了这种风格的用法,但有人用这个构造函数定义向我解释了确切的编程技术。
例如:1
class time_stamp
{
public:
time_stamp(time &t_time)
: m_time(t_time)
{}
~time_stamp()
{
m_time.update(); // as soon as I'm destroyed, update the time
}
private:
time &m_time;
};
例如:2
class threaded_class
{
public:
threaded_class()
: m_stoprequested(false), m_running(false)
{
pthread_mutex_init(&m_mutex);
}
~threaded_class()
{
pthread_mutex_destroy(&m_mutex);
}
/** Some other member declarations */
}
请解释我在上面 2 个示例的以下代码行中使用“:”
time_stamp(time &t_time) : m_time(t_time){} 和
threaded_class(): m_stoprequested(false), m_running(false)
{
pthread_mutex_init(&m_mutex);
}
【问题讨论】:
-
这是一个初始化列表。 stackoverflow.com/questions/926752/…
-
拍手;被引用了,会影响原著吗?
-
哦,我的问题没有给出上述 2 个帖子的搜索结果 :(