After some google search I cannot find an answer to this question. How do I initialize it, and why do I need to?
#include "CalculatorController.h"
CalculatorController::CalculatorController(SimpleCalculator& aModel, ICalculatorView& aView)
{\\(this is the bracket informing me of the error)
fModel = aModel;
fView = aView;
}
header:
#pragma once
#include "ICalculatorView.h"
#include "SimpleCalculator.h"
class CalculatorController
{
private:
SimpleCalculator& fModel;
ICalculatorView& fView;
public:
CalculatorController(SimpleCalculator& aModel, ICalculatorView& aView);
void run();
~CalculatorController();
};
Instead of:
CalculatorController::CalculatorController(SimpleCalculator& aModel, ICalculatorView& aView)
{\\(this is the bracket informing me of the error)
fModel = aModel;
fView = aView;
}
Use
CalculatorController::CalculatorController(SimpleCalculator& aModel, ICalculatorView& aView)
: fModel(aModel),fView(aView)
{
}
fModel and fView are reference member. Different instances of CalculatorController can share the same instances fModel and fView this way, without using nasty pointer.
Reference member have to be initialized on creation. My second code block show how to.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With