好的,成员变量可用于初始化初始化列表中的其他成员变量(注意初始化顺序等).会员职能怎么样?具体来说,这个片段是否符合C++标准?
struct foo{
foo(const size_t N) : N_(N), arr_(fill_arr(N)) {
//arr_ = fill_arr(N); // or should I fall back to this one?
}
std::vector<double> fill_arr(const size_t N){
std::vector<double> arr(N);
// fill in the vector somehow
return arr;
}
size_t N_;
std::vector<double> arr_;
// other stuff
};
Run Code Online (Sandbox Code Playgroud) 我的直觉是不是.我遇到以下情况:
class PluginLoader
{
public:
Builder* const p_Builder;
Logger* const p_Logger;
//Others
};
PluginLoader::PluginLoader(Builder* const pBuilder)
:p_Builder(pBuilder), p_Logger(pBuilder->GetLogger())
{
//Stuff
}
Run Code Online (Sandbox Code Playgroud)
或者我应该更改构造函数并Logger* const从构造的位置传递一个PluginLoader?
现代 C++ 中是否有一种方法可以在从访问指针参数派生的类中初始化 const 值?并且该指针可能为空,所以基本上在检查指针之后?
例如
// Dummy class to pass as argument in Class A through a pointer, just for this given example
class Zero
{
public:
int i = 0;
};
class A
{
public:
A(Zero* z) : isZero(z->i == 0) // in this simple e.g.
// could be: isZero(z == nullptr ? false : z->i), but immagine having a more complex structure instead,
// this kind of verbose way will work,
// but i am asking if …Run Code Online (Sandbox Code Playgroud)