I have the following code:
class Carb {
public:
virtual void calorie() = 0; // pure virtual
}
class Omelette : Carb {
public:
void calorie() {/*do something*/}; // non-virtual
}
class Breakfast {
public:
Breakfast(const Carb &somecarb):
my_carb(somecarb){}
private:
const Carb &my_carb;
}
}
I understand that Carb is a virtual class therefore cannot be an instance, but we can use a pointer from the following stackoverflow questions.
C++ : Cannot declare field to be of abstract type
C++ error: cannot declare field to be of abstract type
However, it also seems to work by declaring a const reference like my code above. My question is, is it valid to have a class field to be a constant reference to a abstract class? Why does it work this way?