Solved

Virtual & Overide?

Posted on 2001-07-16
6
201 Views
Last Modified: 2013-11-18
Now I know override will hide overload member functions in BASE class, there is a furture more question, what happened when I mark fun() as "virtual" like following,
This should generate a "VTBL" for BASE and Derived class but why the override sitll hides overloaded member functions in BASE class???

1 #include <stdio.h>
3
4 class BASE
5 {
6 public:
7 BASE() {};
8 ~BASE() {};
9 virtual fun() { cout << " BASE fun()" << endl; } ;
10 virtual fun(int ) { cout << " BASE fun(int)" << endl; } ;
11 virtual fun(int , int) { cout << " BASE fun(int )" << endl; } ;
12 };
13
14 class DERIVED: public BASE
15 {
16 public:
17 DERIVED() {};
18 ~DERIVED() {};
19 fun() { cout << " DETRIVED fun()" << endl; } ;
20 };
21
22
23 int main()
24 {
25 DERIVED *d1= new DERIVED();
26 BASE *d2= new DERIVED();
27
28 d1->fun(1);  //Compile Error... Why??? fun is Virtual now
29
30 d2->fun(1);  //OK
31 return 0;
32 }





0
Comment
Question by:yuxiliu
[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
  • 6
6 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 200 total points
ID: 6287124
The hiding effect is only seen in the name-lookup algorithm.  This has NOTHING to do with whehtor or not the function is virtual.   Thus in the code

 d1->fun(1);  //Compile Error... Why??? fun is Virtual now

Since d1 is a pointer to a derived objkect, the compiler searches first in the scope of the derived class.  Only if it does not find anything called fun() will it then search in an outer scope (the BASE class).   However if it finds a function called fun() in the derived class, then it stops the search for functions.

continues
0
 
LVL 22

Expert Comment

by:nietod
ID: 6287150
Now consider

>> d2->fun(1);  //OK

In this case the pointer is a pointer to a BASE object, so the compiler performs a search in the scope of the BASE class.  (And no matter what, it will end the search there too, since there are no class scopes outside of that class.)  In this case it finds a function called fun, so it then stops the search for functions in the scope of the BASE class.  It then looks for a function whose signature matches the invokation.  In this case it finds

virtual fun(int ) { cout << " BASE fun(int)" << endl; } ;


continues
0
 
LVL 22

Expert Comment

by:nietod
ID: 6287184
class B
{
public:  
   void fun() { cout << "B:fun()"; }
   void fun(int i) { cout << "B:fun(int)"; }
};

class D1 : public B
{
public:  
   void fun(int) { cout << "D1:fun(int)"; }
};

class D2 : public D1
{
public
void AFunction();
};

consider what the compiler does when it encouters the following in AFunction.

void D2::AFunction()
{
  fun(5);
}

The object is a D2 object, so the compiler starts searching for the function in the scope of the D2 class.    There is no function called "fun" in that scope.  So it proceeds to the outer scope.  This is the D1 scope.  It does find a function called "fun" in this scope.  So now the search for fucntions ends in this scope.  The compiler then looks for the function in this scope, in D2, that matches the signature of the invocation.  However, there is none, so the program is ill-formed.  

No consider.

void D2::AFunction()
{
  fun(5);
}

The object is a D2 object, so the compiler starts searching for the function in the scope of the D2 class.    There is no function called "fun" in that scope.  So it proceeds to the outer scope.  This is the D1 scope.  It does find a function called "fun" in this scope.  So now the search for functions ends in this scope..  The compiler then looks for the function in this scope, in D2, that matches the signature of the invocation.  It finds "fun(int)" , so there is a match.  sot he program is fine.


continues
0
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
LVL 22

Expert Comment

by:nietod
ID: 6287204
Now code like

D2 d2;

d2->fun(5); // Okay;
d2->fun(); // Error.

is the same.  The compiler still searches beginnign with the scope of the D2 class and then continues to the other scopes only if it finds no functions that have the name "fun"  Once any scope is found that has any function of the target name, the search will stop (in success or in failure) with the scope that contains that name.  This is true even if there is an outer scope that does supply the right signature function.


Note that this search has NOTHING to do with virtual functions in any way.  The compiler is simply searching to see if a function exists with the specified name and signature and that the function is accessable (not private for example).  If the function is virtual, the exact override that will be called will be dermined at run-time (unless the compiler can determine it at compile time, often it is possible to do so.)  but that is a seperate issue.  Before it can consider the virtual aspect of the function, it needs to find a declation for the function and for that it must perforn the search I outlined.

continues
0
 
LVL 22

Expert Comment

by:nietod
ID: 6287220
Now you can get around this by using a "using declaration"   Like  In your code you could use

class DERIVED: public BASE
{
public:
   DERIVED() {};
   ~DERIVED() {};

   using BASE::fun;  // Bring the "fun" names in from the BASE class

   fun() { cout << " DETRIVED fun()" << endl; } ;
};

This brings the names of the "fun" functions from the BASE scope to the DERIVED scope.  This allows the compiler's search to find all the "fun()" names that were declared in BASE in the scope of the DERIVED class.
0
 
LVL 22

Expert Comment

by:nietod
ID: 6290861
Has this answered your question?  
0

Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

VM backups can be lost due to a number of reasons: accidental backup deletion, backup file corruption, disk failure, lost or stolen hardware, malicious attack, or due to some other undesired and unpredicted event. Thus, having more than one copy of …
This article shows how to use a free utility called 'Parkdale' to easily test the performance and benchmark any Hard Drive(s) installed in your computer. We also look at RAM Disks and their speed comparisons.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

707 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