0

I am very new to C++ so apologies for the wrong and probably confusion terminology in the title. Would love to know what this is properly called.

I am trying to add an object with an overloaded constructor to another object. However, it always takes the default constructor instead of my overloaded one.

Consider the following scenario. I have a water pump with an optional water level sensor. I have a separate class for my water level sensor.

class WaterLevelSensor{
  private:
    bool _waterLevelOK;
    bool _reversedLogic;
  public:
    WaterLevelSensor();
    WaterLevelSensor(bool reversedLogic);
};

WaterLevelSensor::WaterLevelSensor(){
    _reversedLogic = true;
}

WaterLevelSensor::WaterLevelSensor(bool reversedLogic){
    _reversedLogic = reversedLogic;
}

and a pump class

class Pump{
  private:
    bool _hasWaterSensor = false;
    WaterLevelSensor waterSensor;
  public:
    Pump();
    void addWaterSensor(reversedLogic);
};

Pump::Pump(){}

void Pump::addWaterSensor(reversedLogic){
    WaterLevelSensor waterSensor(reversedLogic);
    _hasWaterSensor = true;
}

If I do the following, the water sensor will still have reversedLogic == true, even though I passed false. I assume this is due to the fact that during the creation of the pump the default constructor of the sensor is already called, passing the reversedLogic = true. How would I solve this? I do realise I could fix it by creating a function like setLogic() that allows to alter the variable. But I am just curious what the proper solution to this would be without creating this.

int main(){
  Pump pump;
  pump.addWaterSensor(false);
  return 0;
}
3
  • 1
    Note that you don't need two constructors. WaterLevelSensor(bool reversedLogic = true); should be enough Commented Dec 6, 2019 at 20:53
  • Also, shouldn't void addWaterSensor(reversedLogic) be: void Pump::addWaterSensor(bool reversedLogic)? Commented Dec 6, 2019 at 20:56
  • Ah I thought I couldn't pass a default value like that, that is very convenient, thanks! And yes indeed it has to be, good catch. Forgot that when I was typing this minimal example code. Commented Dec 6, 2019 at 21:01

2 Answers 2

1

In addWaterSenor, your object is being declared and created in the local scope and will no longer exist immediately after the function returns. Assign the new WaterSensor to waterSensor that already exists as a class member without declaring a new local one.

waterSensor = WaterLevelSensor(reversedLogic);
Sign up to request clarification or add additional context in comments.

Comments

0

You are trying to modify a private class variable in WaterLevelSensor from another class Pump.

Creating a setLogic(...) function in WaterLevelSensor would be the best solution. But as an alternative you could declare Pump as a friend of WaterLevelSensor.

class Pump; // forward declaration

class WaterLevelSensor {
  private:
    friend class Pump; // make Pump a friend
    bool _waterLevelOK;
    bool _reversedLogic;
  public:
    WaterLevelSensor(void);
    WaterLevelSensor(bool reversedLogic);
};

class Pump {
  private:
    bool _hasWaterSensor = false;
    WaterLevelSensor waterSensor;
  public:
    Pump(void);
    void addWaterSensor(bool reversedLogic);
};

void Pump::addWaterSensor(bool reversedLogic)
{
  waterSensor._reversedLogic = reversedLogic;
  _hasWaterSensor = true;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.