class foo{
public:
double v1, v2, v3;
double x, y;
foo(const double &a){
set_var();
x = a;
if(a == v1)
y = 1;
else if(a == v2)
y = 0;
else if(a == v3)
y = 2;
}
void set_var(){
// v1, v2 and v3 will always be the same values as the corresponding double values
// that foo a,b and c are being set to in main()
v1 = 1.4;
v2 = 1.4;
v3 = 1.4;
}
};
And in the main function:
main(){
foo a = 1.2, b = 1.2, c = 1.2;
}
Essentially, what I want this code to do when we are trying to set ‘foo a, b, c’ to the same double value (and hence ‘v1, v2, v3’ are the same value as well) is foo a=1.4 gives a.y=1,
foo b=1.4 gives b.y=0, and foo c=1.4 gives c.y=2
in short pseudocode. Basically, ‘foo a’ should always use the first if clause, ‘foo b’ should always use the second, and ‘foo c’ should always use the third.
As part of this assignment, I MUST implement the main function in that way. It cannot be changed.
My first thought was that even if I’m trying to set foo a,b,c to the same double, that double always takes up a different place in memory, no? So maybe I could somehow pass in the address of double &x in the constructor? Then set that address to represent v1, v2 and v3 instead of using those variables in the first place?
I just need to get the code to do what I outlined a couple of paragraphs above even when the double values are equal.
Since it appears that your question is in regards to a homework assignment, I’m going to point you in the direction of static member variables which are, in short, class variables that are shared between all instances of a given class. You can use one or more of these variables to track the progress of the instantiation of your instances.