Reputation: 69
I'm relatively new to c++ so I'm getting used to the scope of functions and variables in this environment. One issue I can't solve is being able to call a function defined in a subclass of a class which declares this function as pure virtual. Here's my class structure:
class Clock{
public:
virtual void Tick() = 0;
std::string Print();
//Mutators
int Seconds();
int Minutes();
int Hours();
void setTime(int secs, int mins, int hours);
//Accessors
int getSecs(){ return _secs; }
int getMins(){ return _mins; }
int getHrs(){ return _hours; }
private:
int _secs, _mins, _hours, _days = 0;
};
class NormalClock : public Clock{
public:
void Clock::Tick();
private:
};
class AlarmClock : public Clock{
public:
void Clock::Tick();
bool CheckAlarm();
void SetAlarmTime(int hrs, int mins, int secs);
int GetAHrs();
int GetAMins();
int GetASecs();
private:
int _alarmhrs, _alarmmins, _alarmsecs;
};
In my source file I want to define a body for the Tick()
function in the AlarmClock
class. But within this function I want to call the Tick()
function from it's superclass, the NormalClock
class. My issue is that when I do so without defining any objects to work off of, I can't call the superclass Tick()
function as my IDE (VS2013) thinks I'm referring to the Tick()
from the current class (the AlarmClock
subclass). I looked around on the web and determined that it would probably require the use of the using
keyword, but I've been unsuccessful in implementing it properly. Here's the function I'm trying to build for reference:
void AlarmClock::Tick(){
NormalClock::Clock::Tick();
if (this->CheckAlarm()){ cout << "\nAlarm! @ " << this->Print() << "\n\n"; }
}
There's no errors in VS when calling the function as above, but the compiler complains of a static reference to a non-static member, which is understandable.
Upvotes: 1
Views: 1238
Reputation: 69
Thanks everyone, looks like it was a simple case of changing the base class declaration to NormalClock
in the definition of the class AlarmClock
. VS2013 was skipping over the NormalClock
class and observing only the Tick()
pure virtual function in Clock
, which is why I couldn't call NormalClock::Tick()
as intended in the body of the AlarmClock::Tick()
function.
Upvotes: 0
Reputation: 1356
You were close, but your code has a few problems.
Upvotes: 2
Reputation: 36337
NormalClock::Clock::Tick();
looks a lot like you mean the right thing. Now make your AlarmClock
actually inherit from NormalClock
, and then just NormalClock::Tick()
away :)
Upvotes: 0
Reputation: 9609
AlarmClock
does not derive from NormalClock
so you are calling the method statically (there is no instance of NormalClock
to be used).
Upvotes: 0