【发布时间】:2014-03-20 16:09:35
【问题描述】:
我有一个要转换的课程:
class MyClass
{
public::
void foo( void )
{
static const char* bar[][3] = { NULL };
func( bar );
}
};
现在我想让 bar 成为一个成员变量,但是因为第一个维度的大小我不能。我也不能将const char** bar[3] 传递给void func( const char* param[][3] )。是否有我不知道的解决方法,或者这是我必须使用方法static 的情况?
编辑以回复Jarod42
匹配bar的初始化是我这里的问题。我认为我至少应该能够在 ctor 主体中完成此操作,如果不是 ctor 初始化列表。下面是一些测试代码:
static const char* global[][3] = { NULL };
void isLocal( const char* test[][3] )
{
// This method outputs" cool\ncool\nuncool\n
if( test == NULL )
{
cout << "uncool" << endl;
}
else if( *test[0] == NULL )
{
cout << "cool" << endl;
}
}
class parent
{
public:
virtual void foo( void ) = 0;
};
parent* babyMaker( void )
{
class child : public parent
{
public:
virtual void foo( void )
{
static const char* local[][3] = { NULL };
isLocal( local );
isLocal( global );
isLocal( national );
}
child():national( nullptr ){}
private:
const char* (*national)[3];
};
return new child;
}
int main( void )
{
parent* first = babyMaker();
first->foo();
}
【问题讨论】:
标签: c++ multidimensional-array static dynamic-arrays member-variables