A problem with Diamond problem
-
I have a base class A which is being inherited later & in one inherited class i had OVERLOADED one of the base class function. The code snippet is given below. class A{ public: virtual void fun(){ cout<<"A"; } }; class B: virtual public A{ public : void fun(){ cout<<"k"; } }; class C: virtual public A{ public: int fun(int num){ cout<<"C"; } }; class D: public C, public B{ }; void main(){ D dObj; dObj.fun(4); } Can anyone tell me why i am getting an "ambigous" error in the fn call?
-
I have a base class A which is being inherited later & in one inherited class i had OVERLOADED one of the base class function. The code snippet is given below. class A{ public: virtual void fun(){ cout<<"A"; } }; class B: virtual public A{ public : void fun(){ cout<<"k"; } }; class C: virtual public A{ public: int fun(int num){ cout<<"C"; } }; class D: public C, public B{ }; void main(){ D dObj; dObj.fun(4); } Can anyone tell me why i am getting an "ambigous" error in the fn call?
thenewbee wrote:
dObj.fun(4);
The error came because the compiler (me also)dosent understand which function it should actually call. function of A, or B or C. Actually which class' function do u expect to get executed?
nave [OpenedFileFinder]
-
thenewbee wrote:
dObj.fun(4);
The error came because the compiler (me also)dosent understand which function it should actually call. function of A, or B or C. Actually which class' function do u expect to get executed?
nave [OpenedFileFinder]
-
:laugh: The call dObj.fun(4) shld call C::fun(int) Thats wt we had learned from the concept of Overloading. . Am i correct??
thenewbee wrote:
Am i correct??
no. First can u tell me why didnt you expect B::fun(int) to get called. Both B and C are base classes of D. Isnt it..? is it because C is your favourite :laugh: ?
nave [OpenedFileFinder]
-
thenewbee wrote:
Am i correct??
no. First can u tell me why didnt you expect B::fun(int) to get called. Both B and C are base classes of D. Isnt it..? is it because C is your favourite :laugh: ?
nave [OpenedFileFinder]
-
virtual void fun(){ cout<<"A"; } }; class B: virtual public A{ public : void fun(){ cout<<"k"; } }; Its not becoz C is my Fav. . :) The base class B dont have fun(int). But C have. & how will B get the fun(int)??