Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Why doesn't the compiler see overloaded base class functions?

Posted on 2002-05-15
7
Medium Priority
?
201 Views
Last Modified: 2010-04-02
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!
0
Comment
Question by:magenta
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 

Author Comment

by:magenta
ID: 7012178
I should have pointed out that the compiler complains about the following lines:

     d.Do( string() );
     d.Do( "foobar", 6 );

It seems to only see the Derived methods.
0
 
LVL 6

Accepted Solution

by:
thienpnguyen earned 200 total points
ID: 7012402
In subclass, if you have a overload funtion that is same name with a fucntion in base class, then that function in base class will be hide .

0
 
LVL 6

Expert Comment

by:thienpnguyen
ID: 7012417
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;
}
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:thienpnguyen
ID: 7012427
0
 
LVL 3

Expert Comment

by:MDarling
ID: 7012450
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;
}
0
 

Author Comment

by:magenta
ID: 7012541
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?
0
 
LVL 11

Expert Comment

by:griessh
ID: 7178762
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: http://www.experts-exchange.com/commspt/


PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question