Create type using class of a structure, and use the same type within the class

0

Currently I need to create a collection of classes representing nodes (fragments) of the AST (abstract abstract tree) of an interpreter. Now, for example, I'll give an overview of C ++ templates and try to declare two members ( left and right ) for a ASSIGNOP class, which would be both nodes.

#ifndef AST_H
#define AST_H

struct ast
{
    template<typename T>
    class ASSIGNOP
    {
    public:
        T *left;
        T *right;
    }
} Ast;

#endif

I did not test this code because I have to learn how to use make yet ...

So, the problem is that ASSIGNOP members are not considered nodes (I still do not know much about templates). I need to make them forced to be one of the classes within Ast . How could I do that?

    
asked by anonymous 17.04.2017 / 03:15

1 answer

1

I do not know if I understood your question well, but you could do something like this:

struct ast
{
    class VARIABLE
    {
    public:
        double value;
    };

    template<typename T>
    class ASSIGNOP
    {
    public:
        T *left;
        T *right;
    };

    ASSIGNOP<VARIABLE> AssignOp;
} Ast;

int main()
{
    Ast.AssignOp.left = new ast::VARIABLE{ 1.23 };
    // ...
    return 0;
}

Although I think this code needs several improvements in both class / variable naming terms and in terms of code organization and maintenance ( left / right being public members of ASSIGNOP , Ast being a global variable etc.).

    
19.04.2017 / 18:03