Question

I have two structs:

    struct port
{
    bool isOutput;
    bool isConnected;
    int connwires;
};

struct node
{
    port p;
    vector<Wire*> w;
};

And I have:

    node *nodes;

in my class. The question is how to initialize the port member (p) of all n node structs created by:

    nodes= new node[n];

statement in the class constructor.

(I was defining port struct like this:

    struct port
{
    bool isOutput=0;
    bool isConnected=0;
    int connwires=0;
};

but its invalid in "ISO C++". )

Thanks.

Was it helpful?

Solution

You need to provide a default constructor for port to initialize it's members automatically

struct port
{
    port() :
        isOutput(false),
        isConnected(false),
        connwires(0)
    { }

    bool isOutput;
    bool isConnected;
    int connwires;
};

Note that your last code is valid and does what you expect since C++11.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top