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

Asked

Viewed 37 times

0

Currently I need to create a collection of classes that represent nodes (fragments) of the AST (abstract syntactic tree) of an interpreter. Now, for example, I gave an overview of the C++ templates and tried to declare two members (left and right) for a class ASSIGNOP, 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 haven’t tested this code because I have to learn to use make yet...

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

1 answer

1

I don’t know if I understand your question, but you could do something like:

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 both in terms of class/variable nomenclature and in terms of code organization and maintenance (left/right being public members of ASSIGNOP, Ast being a global variable etc.).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.