First Size Non-Dimensional Member of the Class

I have a class that I am converting:

class MyClass
{
public::
    void foo( void )
    {
        static const char* bar[][3] = { NULL };
        func( bar );
    }
};

Now I want to make bar a member variable, but since the first dimension is not supported, I cannot. I also can not pass const char** bar[3]in void func( const char* param[][3] ). Is there a workaround for this that I don't know about, or is it a situation where I have to use a method static?

Edit in response to Jarod42

Initialization matching baris my problem here. I think that at least I could do this in the ctor package, if not for the ctor initialization list. Here are some test codes:

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();
}
0
1

const char* bar[][3] const char** bar[3], const char* (*bar)[3].
- :

class MyClass
{
public:
    MyClass() : bar(nullptr) {}

    void foo() { func(bar); }
private:
    const char* (*bar)[3];
};

typedef :

class MyClass
{
public:
    typedef const char* bar_t[3];
public:
    MyClass() : bar(new bar_t[2]) {
        for (int j = 0; j != 2; ++j) {
            for (int i = 0; i != 3; ++i) {
                bar[j][i] = nullptr;
            }
        }
    }
    ~MyClass() { delete [] bar; }

    void foo() { func(bar); }

private:
    MyClass(const MyClass&); // rule of three
    MyClass& operator =(const MyClass&); // rule of three
private:
    bar_t* bar;
};

class MyClass
{
public:
    typedef const char* bar_t[3];
public:
    MyClass() { for (int i = 0; i != 3; ++i) { bar[0][i] = nullptr; } }

    void foo() { func(bar); }

private:
    bar_t bar[1];
};
+1

Source: https://habr.com/ru/post/1533607/


All Articles