[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Inheritance issue

Posted on 2003-11-06
6
Medium Priority
?
208 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 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

834 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