magenta
asked on
Why doesn't the compiler see overloaded base class functions?
Why doesn't the compiler see overloaded base class functions? I cannot explain why the follwing is an error:
#include <string>
#include <iostream>
using namespace std;
class Base
{
public:
virtual void Do( const string& )
{
cout << "Base::Do( string )" << endl;
}
virtual void Do( const char* const data,
const long length )
{
cout << "Base::Do( char*, long )" << endl;
}
};
class Derived : public Base
{
public:
virtual void Do( const int s )
{
cout << "Base::Do( int )" << endl;
}
};
int main(int argc, char* argv[])
{
Derived d;
d.Do( 5 );
d.Do( string() );
d.Do( "foobar", 6 );
return 0;
}
Why doesn't the compiler "see" the base class versions of Do()? The only way I can get this to compile is to qualify the calls by adding "Base::", like so:
d.Base::Do( string() );
d.Base::Do( "foobar", 6 );
Can someone explain to me why this is the case? Thanks!
#include <string>
#include <iostream>
using namespace std;
class Base
{
public:
virtual void Do( const string& )
{
cout << "Base::Do( string )" << endl;
}
virtual void Do( const char* const data,
const long length )
{
cout << "Base::Do( char*, long )" << endl;
}
};
class Derived : public Base
{
public:
virtual void Do( const int s )
{
cout << "Base::Do( int )" << endl;
}
};
int main(int argc, char* argv[])
{
Derived d;
d.Do( 5 );
d.Do( string() );
d.Do( "foobar", 6 );
return 0;
}
Why doesn't the compiler "see" the base class versions of Do()? The only way I can get this to compile is to qualify the calls by adding "Base::", like so:
d.Base::Do( string() );
d.Base::Do( "foobar", 6 );
Can someone explain to me why this is the case? Thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
For fixing the bug, you can do as following
class Derived : public Base
{
public:
using Base::Do; // <-------- new code
virtual void Do( const int s )
{
cout << "Base::Do( int )" << endl;
}
};
int main(int argc, char* argv[])
{
Derived d;
d.Do( 5 );
d.Do( string() );
d.Do( "foobar", 6 );
return 0;
}
class Derived : public Base
{
public:
using Base::Do; // <-------- new code
virtual void Do( const int s )
{
cout << "Base::Do( int )" << endl;
}
};
int main(int argc, char* argv[])
{
Derived d;
d.Do( 5 );
d.Do( string() );
d.Do( "foobar", 6 );
return 0;
}
Look at the paragraph "You Can Run, but You Can’t Hide" in the url :
http://www.cuj.com/articles/2001/0104/0104e/0104e.htm?topic=articles
The code
http://www.cuj.com/articles/2001/0104/0104e/0104el2.htm?topic=articles
http://www.cuj.com/articles/2001/0104/0104e/0104e.htm?topic=articles
The code
http://www.cuj.com/articles/2001/0104/0104e/0104el2.htm?topic=articles
This is not meant to trample on thienpnguyen's answer.
Just thought I'd post the full listing as I got it to compile on M$ VC6.0.
Nice links Thien! Almost perfect for this question.
#include <string>
#include <iostream>
using namespace std;
class Base
{
public:
virtual void Do( const string& )
{
cout << "Base::Do( string )" << endl;
}
virtual void Do( const char* const data,
const long length )
{
cout << "Base::Do( char*, long )" << endl;
}
};
class Derived : public Base
{
public:
Base::Do;
virtual void Do( const int s )
{
cout << "Base::Do( int )" << endl;
}
};
int main(int argc, char* argv[])
{
Derived d;
d.Do( 5 );
d.Do( string() );
d.Do( "foobar", 6 );
return 0;
}
Just thought I'd post the full listing as I got it to compile on M$ VC6.0.
Nice links Thien! Almost perfect for this question.
#include <string>
#include <iostream>
using namespace std;
class Base
{
public:
virtual void Do( const string& )
{
cout << "Base::Do( string )" << endl;
}
virtual void Do( const char* const data,
const long length )
{
cout << "Base::Do( char*, long )" << endl;
}
};
class Derived : public Base
{
public:
Base::Do;
virtual void Do( const int s )
{
cout << "Base::Do( int )" << endl;
}
};
int main(int argc, char* argv[])
{
Derived d;
d.Do( 5 );
d.Do( string() );
d.Do( "foobar", 6 );
return 0;
}
ASKER
Very intesting...I'm going to have to sit down and read that article. I should probably have the C++ standard nearby...
However, before I do that, answer this question. If a derived class by definition can be treated the same as a base class, then isn't the code above example of this rule being broken?
However, before I do that, answer this question. If a derived class by definition can be treated the same as a base class, then isn't the code above example of this rule being broken?
Dear magenta
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity, I will suggest to accept
"thienpnguyen"
comment(s) as an answer.
"refund the points and delete this question"
since nobody had a satisfying answer for you.
since you never gave more feedback.
PAQ at zero points.
If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points. The link to the Community Support area is: https://www.experts-exchange.com/commspt/
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity, I will suggest to accept
"thienpnguyen"
comment(s) as an answer.
"refund the points and delete this question"
since nobody had a satisfying answer for you.
since you never gave more feedback.
PAQ at zero points.
If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points. The link to the Community Support area is: https://www.experts-exchange.com/commspt/
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
ASKER
d.Do( string() );
d.Do( "foobar", 6 );
It seems to only see the Derived methods.