-
September 5th, 2012, 09:16 AM
#1
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
-
September 5th, 2012, 09:49 AM
#2
Re: Why isn't a base class function visible?
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();
}
-
September 5th, 2012, 09:52 AM
#3
Re: Why isn't a base class function visible?
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
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
September 5th, 2012, 01:57 PM
#4
Re: Why isn't a base class function visible?
Thanks very much for your reminders and suggestions.
RobR
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|