Reputation: 449
I created an abstract Light
class with fields and methods common for all the lights and now I try to derive a Directional Light
from it.
class Light
{
public:
unsigned int strength;
Color color;
unsigned int index;
Light() {};
virtual ~Light() = 0;
virtual pointLuminosity() = 0;
};
class DirectionalLight : public Light
{
public:
Vector direction;
DirectionalLight(const unsigned int &_strength, [...] ): strength(_strength), [...] {}
};
The above code results in an error:
error: class 'DirectionalLight' does not have any field named 'strength'
What is the proper way to derive all the fields from Light
class and use them in the DirectionalLight
objects?
Upvotes: 1
Views: 123
Reputation: 6467
You can't do that in initializer list since strength
is not member of DirectionalLight
. You have to initialize derived member in the body of constructor or call base class constructor in the initializer list of the derived class constructor.
For example:
DirectionalLight(const unsigned int &_strength): { strength = _strength; }
Or:
Light(int _strength) : strength(_strength) {}
...
DirectionalLight(const unsigned int &_strength): Light(_strength) { }
Second option is prefered, also strength
in Light
should be protected, so the encapsulation is not destroyed.
Upvotes: 2
Reputation: 87944
You can use strength anywhere but an initialiser list. This works
DirectionalLight(const unsigned int &_strength) { strength = _strength; }
Alternatively you can add a constructor to Light
class Light
{
public:
unsigned int strength;
Light(unsigned s) : strength(s) {}
};
DirectionalLight(const unsigned int &_strength) : Light(_strength) {}
Upvotes: 4