mardi 26 avril 2016

Function uses object and object uses function

I essentially have a cyclic dependency problem where a function uses an object object and the object uses said function. Is there any way to resolve this without working around it?

//function that uses struct
void change_weight(Potato potato,float byX) { potato.weight+=byX; }
//said struct that uses said function
struct Potato
{
    float weight=0.0;
    Potato(float weightin) { change_weight(weightin); }
}

Note that I understand this example is silly, but this example only contains the "essence of the problem" which has come up in much more complex situations where I would sometimes not know how I would work around it, and it would be very convenient to just be able to do it when I can't see a way around. I am asking if there is a way to do this without working around it.

Aucun commentaire:

Enregistrer un commentaire