I have to implement state design pattern in c++ for sql recordset
I would like to implement movefirst(),moveNext(),movelast(),movePrevious() function in c++
RecExtractionSystem.h:
class RecExtractionSystem
{
States *m_CurrentState;
public:
RecExtractionSystem();
bool ProcessCurrentRec();
bool ProcessNextRec();
bool ProcessFirstRec();
bool ProcessPrevRec();
bool ProcessLastRec();
void SetCurrentState(States* currentState);
};
States.h:
class States
{
public:
virtual bool ProcessCurrentRec(RecExtractionSystem *pRecExtractionSystem);
virtual bool ProcessNextRec(RecExtractionSystem *pRecExtractionSystem);
virtual bool ProcessFirstRec(RecExtractionSystem *pRecExtractionSystem);
virtual bool ProcessPrevRec(RecExtractionSystem *pRecExtractionSystem);
virtual bool ProcessLastRec(RecExtractionSystem *pRecExtractionSystem);
};
class StopProcessingRec: public States
{
public:
bool ProcessCurrentRec(RecExtractionSystem *pRecExtractionSystem);
};
class ProcessinCurrentRec: public States
{
public:
bool ProcessFirstRec(RecExtractionSystem *pRecExtractionSystem);
bool ProcessNextRec(RecExtractionSystem *pRecExtractionSystem);
bool ProcessPrevRec(RecExtractionSystem *pRecExtractionSystem);
bool ProcessLastRec(RecExtractionSystem *pRecExtractionSystem);
};
Void main()
{
RecExtractionSystem *pRecExtractionSystem = new RecExtractionSystem();
pRecExtractionSystem->ProcessNextRec();
}
When I call the ProcessNextRec(), it is calling the function of
State::ProcessNextRec()
But it should call the
ProcessinCurrentRec::ProcessNextRec().
What is wrong here?
You'll need to add the override key to each virtual function you reimplement from the base class
https://en.cppreference.com/w/cpp/language/override
class ProcessinCurrentRec: public States
{
public:
bool ProcessNextRec(RecExtractionSystem *pRecExtractionSystem) override;
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.