Solved

Inheritance issue

Posted on 2003-11-06
6
202 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
  • 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 100 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
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.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
Quicksort a dynamic deque 33 73
ASP.net build a IF/Then Walkthrough Guide 1 235
Indy 10 not Receiving UDP broadcast 3 22
C++ Code Issue 4 25
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

856 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