Solved

Virtual & Overide?

Posted on 2001-07-16
6
196 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
  • 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Storage devices are generally used to save the data or sometime transfer the data from one computer system to another system. However, sometimes user accidentally erased their important data from the Storage devices. Users have to know how data reco…
A Bare Metal Image backup allows for the restore of an entire system to a similar or dissimilar hardware. They are highly useful for migrations and disaster recovery. Bare Metal Image backups support Full and Incremental backups. Differential backup…
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 viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

776 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