Solved

Inheritance issue

Posted on 2003-11-06
6
200 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maze travler 6 51
Quicksort a dynamic deque 33 66
SendMessage fails while PostMessage works 2 108
Embarcadero C++ Builder XE10.1 Berlin red arrow Indicator 2 20
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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…

864 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

24 Experts available now in Live!

Get 1:1 Help Now