Solved

Inheritance issue

Posted on 2003-11-06
6
197 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
Comment Utility

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
Comment Utility
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
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 9

Expert Comment

by:_ys_
Comment Utility
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
Comment Utility
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
Comment Utility

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

772 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now