C ++:新手初始化程序列表问题 [英] C++: newbie initializer list question
问题描述
新手在这里.我正在查看公司代码.
Newbie here. I am looking at company code.
似乎在类A中没有成员变量,但是在类A的构造函数中,即使类A不包含任何类型B的成员变量(或根本不包含任何成员变量!),它仍会初始化对象B.
It appears that there are NO member variables in class A yet in A's constructor it initializes an object B even though class A does not contain any member variable of type B (or any member variable at all!).
我想我什至不了解它,甚至不能问一个问题...所以这是怎么回事!?我的直觉是,在尝试初始化变量之前,您需要一个变量.在没有对象的情况下如何初始化对象(或有什么好处)?
I guess I don't understand it enough to even ask a question...so what's going on here!? My intuition is that you need a variable before you even try to initialize it. How is it possible (or what good does it do) to initialize an object without having the object?
.h:
class A: public B
{
public:
A(bool r = true);
virtual ~A;
private:
}
.cpp:
A::A(bool r) : B(r ? B::someEnumeration : B::anotherEnumeration)
{
}
A::~A()
{
}
请帮助.
谢谢, 杰布
推荐答案
Class A
(publicly) inherits from class B
:
class A: public B
用参数初始化基类的唯一方法是通过初始化程序列表.
The only way to initialize a base class with parameters is through the initializer list.
这篇关于C ++:新手初始化程序列表问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!