?
Solved

Inheritance issue

Posted on 2003-11-06
6
Medium Priority
?
205 Views
Last Modified: 2010-04-02
- -  - -  - -  - -  - -  - -  - -  - -  CODE:  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -


class CBaseTestClass
{
public:
      CBaseTestClass(){};
      ~CBaseTestClass(){};
      virtual void Overlay(BYTE* pBits, int nWidth, int nHeight)      {};
      virtual void Overlay(BYTE* pBits)      {};
};

class CTestClass : public CBaseTestClass
{      
public:
      CTestClass(){};
      ~CTestClass(){};
      virtual void Overlay(BYTE* pBits){};
};





if I then do the following:


      CTestClass test;

      test.Overlay(pBits, 10, 10);


I get:    

        error C2660: 'Overlay' : function does not take 3 parameters



Why? What must I do to make this possible?






0
Comment
Question by:jhattingh
[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
  • 3
  • 2
6 Comments
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9694092

A couple of things

First of all, In case of Inheritance, whenevr u declare a function in derived class with the same name as that of the Base class [even with different number/type of arguments], the base class function becomes hidden. in this case, For u to have access to every function available in base class, you would have to declare each base class function [which u need] in the derived class as well

U are expecting overloading to happen but overloading always happens in same context, at same level.
In Inheritance, the context changes, u are operating at two difefrent levels



0
 
LVL 1

Author Comment

by:jhattingh
ID: 9694123
I understood very little of what you explained. Could you give me examples of what you are explaining?
0
 
LVL 10

Accepted Solution

by:
Sys_Prog earned 400 total points
ID: 9694250
OK

Consider a simple example

class Base
{
public:
     Base() {} ;
     ~Base() {} ;
     void f1 (int a, char b) {
        cout << "Base 2 arg" ;
     }    
};

class Derived : public Base
{    
public:
     Derived () {} ;
     ~Derived () {} ;
     void f1 (int a, char b) {
       cout << "Derived 2 arg" ;
     }    
     void f1 ( int a )  {
        cout << "Derived 1 arg" ;
     }
};



int main ( ) {
  Derived d ;
  d.f1(1,'a') ;                        // Compile time Error since this is no longer available
  d.f1 ( 1 ) ;                  // OK
  system ( "PAUSE" ) ;
  return 0 ;
}

Now in the above example, the 2nd line in main gives an error since f1 ( int, char ) is not available in derived. This is becuase u have declared a new function in derived with the same name as that of one of the functions in base. Thus all the base functions with the same name get hidden. This may be termed as shadowing.

To have access to 2 arg version, you will to declare the two arg version again in derived. Actually whenever U define a function in derived having the same name as one of the functiions  in Base, then all the base functions having the same name become inaccessible to derived class. Thus OVERLOADING does not happen between two inheritance levels/context, It happens in the same context

HTH

Amit

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 9

Expert Comment

by:_ys_
ID: 9694262
Due to the presence of CTestClass::Overlay, all base class Overlay functions, effectively, no longer exist - technically it's known as 'hiding'.

You can implictly scope every invocation:
test.CBaseTestClass::Overlay(pBits, 10, 10);

But this is tedious, and easy to forget.

An easier way, and one less likely to forget, is to bring the CBaseTestClass declarations of Overlay back into scope.

class CTestClass : public CBaseTestClass
{    
public:
    using CBaseTestClass::Overlay;

public:
     CTestClass(){};
     ~CTestClass(){};
     virtual void Overlay(BYTE* pBits){};
};
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9694271
In your case, u have also declared the functions as virtual

Actually the virtual feature won't help you in your case

Virtual functions are useful when u need dynamic binding. i.e. the base class pointer invoking the function of derived class.

HTH

Amit
0
 
LVL 1

Author Comment

by:jhattingh
ID: 9694391

ok...

Amit: I can accept your explanation, BUT you rendered an example different to mine. You see, I did not "overwrite/shadow" the 3-arg version.. and yet it STILL wasn't visible..

Why is that?

Ah.... I've just re-read your explanation.. you said:

<< [even with different number/type of arguments], the base class function becomes hidden >>

I see.. Wow.. I can't believe I didn't bump into this issue in the last 8 years of c++ dev .. !!!!!!

I feel like a beginner all of a sudden..

Thanks for the feedback.


ALSO: Thanks to _y_ .. your solution is cool.
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

800 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