2

I am trying to port a nested struct code in C into C++. I know that in C++ the inner structure type will be a member of outer struct type. In my case, I have an inner struct that has no type (marked with *), and I do not know how to fully specify struct2 to the compiler.

typedef struct struct1
{
    struct // *
    {
        struct struct2
        {
            int bla;
            int bla2;
        } *array;

        int bla3;

    } list;

    int bla4;

} struct1_t;


int main()
{
    struct1_t *st = new struct1_t();

    st->bla4 = 10;
    st->list.bla3 = 45;

    // ?
    st->list.array = new struct1_t::struct2();


    return 0;
}

Edit: the structure code above is generated automatically by the ASN1 compiler and I can not make any changes to it.

ManiAm
  • 1,759
  • 5
  • 24
  • 43
  • If you are going to use the C++ in a C++ manner then consider abandoning that C `typedef` construct. Prefer alias declarations to typedefs. Structs are classes in C++. – Ron Aug 23 '17 at 19:47
  • and structs are types anyway in c++ – pm100 Aug 23 '17 at 19:47
  • 1
    It is not "a struct with no type", it is anonymous struct type. But it is unclear what is your problem exactly. Just write normal definitions with proper names separated from variable declarations. – user7860670 Aug 23 '17 at 19:48

1 Answers1

3

This works for me in C++11:

st->list.array = new decltype( struct1_t::list )::struct2;
mark
  • 5,269
  • 2
  • 21
  • 34