vendredi 29 mars 2019

passing struct by reference in c++ doesnt update the value

i am passing a struct by reference to one function of a class and storing it there in a vector.

Then passing the same struct by reference to another function and performing some computation on data member of struct. This data member gets updated in the original struct but not in the ones stored in the vector.

(Sorry for any mistakes, new to c++, new to stack overflow). Please help.

//Structure description
Struct node{

    int x;
    int y;
    int cal; //value to be updated by func

};

int main(){

    treestr * tree= new treestr(); //create object
    node n={1,5,0}
    tree->insert(n);
    int node.cal=tree->func(n);
    return 0;

}

//class description
class treestr{

    insert(node&){
        //store nodes
    }

    func(node&){
        //calculations
        return node.cal;
    }

};

The cal gets updated in the main function but not in the class where I have stored.

Aucun commentaire:

Enregistrer un commentaire