Why isn't a base class function visible?
Greetings!
I have the following code:
Base class header file:
Code:
class ADODIRECT_API CADODirectRecordset : public CADORecordset
{
public:
long GetLastID(const CCOMString& sequenceName);
}
Derived class header file:
Code:
class _OPERATIONSPLANNERSET_CLASS_EXPORT COperationsTaskSet :
public CADODirectRecordset
{
public:
long GetLastID();
}
Derived class implementation:
Code:
long COperationsTaskSet::GetLastID()
{
return GetLastID(_T("operations_task_id_seq"));
}
When I compile this, my compiler complains that COperationsTaskSet::GetLastID() does not accept 1 argument. I had expected that it would see the one-argument version of GetLastID() defined in the base class. Why doesn't it?
If I qualify the function name with the base class name, it works:
Code:
long COperationsTaskSet::GetLastID()
{
return CADODirectRecordset::GetLastID(_T("operations_task_id_seq"));
}
I'm sure I learned about this many years ago, and forgot it. Thank you for refreshing my memory.
RobR
Re: Why isn't a base class function visible?
Quote:
Originally Posted by
RobR
Why isn't a base class function visible?
The derived class member of the same name hides the base class member. The typical solution is a using declaration:
Code:
class _OPERATIONSPLANNERSET_CLASS_EXPORT COperationsTaskSet : public CADODirectRecordset
{
public:
using CADODirectRecordset::GetLastID;
long GetLastID();
}
Re: Why isn't a base class function visible?
Quote:
Originally Posted by
RobR
When I compile this, my compiler complains that COperationsTaskSet::GetLastID() does not accept 1 argument. I had expected that it would see the one-argument version of GetLastID() defined in the base class. Why doesn't it?
If I qualify the function name with the base class name, it works:
This is called name hiding. Basically, the compiler looks in the enclosing scope for the function GetLastID. If it doesn't find it, it looks in the base class (and so on), but if it does find any function named GetLastID then it stops looking for other overloaded functions in further base classes. You can avoid this by adding a using declaration in the derived class.
Code:
class _OPERATIONSPLANNERSET_CLASS_EXPORT COperationsTaskSet :
public CADODirectRecordset
{
public:
long GetLastID();
using CADODirectRecordset::GetLastID;
};
Edit: laserlight really is as fast as the speed of light :D
Re: Why isn't a base class function visible?
Thanks very much for your reminders and suggestions.
RobR