c++ : regarding (inheritence)base class and derived class
hey! is there any one to solve my small conceptual problem:
here is c++ classes : class A { private : int data1; public : function1(); function2(); }; class B { private : int data2; public : function1(); function3(); }; Now I wanted a new class C to be derived from both the classes A and B something like this : class C : public class A , public class B { }; now my class C geting confused with function1(); ( whether it is the one from class A or class B : how to solve this problem. is there any way. |
I'd say your safest bet is to overload it. Depending on the compiler implementation, etc. you may have the function called from class A, class B, or the compiler might barf on you.
However, if I were going to make a bet on which function it would be, I'd say A. since C/C++ is "read backwards" you're first deriving from B, and second deriving from A. Aaron |
The problem is that different compilers may compile overloaded functions differently and it may cause unexpected problems. I suggest, if it is possible in your case, to move function1 out of classes and make it a friend of both, or only class C, depending on what is more convienient for you.
|
Re: c++ : regarding (inheritence)base class and derived class
Quote:
suit your needs ... Did you consider scope resolution? class C : public class A , public class B { A.function1(); }; Cheers, Tink [ Puny programmer :} ] |
Classic Multiple Inheritance
Multiple inheritance causes this type of ambiguity in C++. Most folks avoid it like the plague.
Two ways around it. Reference the correct method using: C x = new C; x->A::function1(); x->B::function1(); Or make two new functions in Class C. class C : public class A , public class B { Afunction1() { A::function1(); } Bfunction1() { B::function1(); } }; |
Hi
i am not using function1() in my derived class C so how to specify not to consider thi fun(if it is possible). or even if it considers one of these class function1() no problem. thing is that how to specify take from this class i can't use class resolution operator because i am not using this method in my derived class thanks ramakrishna |
edreddy,
I am not sure you can specify what function to consider. Even #pragma will probably not help. Whether you like it or not, it's always inherited. Use the code put forth by malicious to specify exactly which func- tion you want to use. Hope this helps, Ravi |
All times are GMT -5. The time now is 10:00 PM. |