C++ Daimond Problem
-
What is the solution of the diamond problem of Inheriting Classes. A class is inheritting from 2 base classes and both the base classes have a function/method with the same name. From the object of the child class how we can call a function of the one base class.
-
What is the solution of the diamond problem of Inheriting Classes. A class is inheritting from 2 base classes and both the base classes have a function/method with the same name. From the object of the child class how we can call a function of the one base class.
If the functions have the same name but they get different parameters... only the function that fits the parameters should be called. Function (int Par1, CString Par2); Function (double Par1, int Par2); is not possible to call both at once although they have the same name. If both functions get the same number and typ of parameters, in the same order... then let's wait to another explanation from the high level users and I will learn it as well ;)
Greetings. -------- M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you “The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson Rating helpfull answers is nice, but saying thanks can be even nicer.
-
What is the solution of the diamond problem of Inheriting Classes. A class is inheritting from 2 base classes and both the base classes have a function/method with the same name. From the object of the child class how we can call a function of the one base class.
Hope you are aware of run time polymorhism. Keep the same name function/method as virtual function in the base class. For the derived class object pointer allocate the corresponding base class (whose function needs to be invoked) then call the function resulting the corresponding base class get invoked. See below to make things clear
class A
{
public:
virtual void show(){cout<<"Class A invoked"};
}
class B
{
public:
virtual void show(){cout<<"Class B invoked"};
}
class C: public A, public B
{
}main()
{
C* ptr = new A;
ptr->show(); // "Class A invoked" will be printed
C* ptr1 = new B;
ptr1->show(); // "Class B invoked" will be printed
} -
What is the solution of the diamond problem of Inheriting Classes. A class is inheritting from 2 base classes and both the base classes have a function/method with the same name. From the object of the child class how we can call a function of the one base class.
Muhammad Zeeshan wrote:
A class is inheritting from 2 base classes and both the base classes have a function/method with the same name.
The class structure is as follows?
Base1 Base2 \\ / \\ / \\ / Derived
If this is the class structure, then its not a classic C++ diamond problem.
Muhammad Zeeshan wrote:
From the object of the child class how we can call a function of the one base class.
Just prefix the base class name before the call. See the code snippet.
Derived DerivedObject;
DerivedObject.**Base1::**CommonFunction();// If its allocated as pointer.
Derived* pDerivedObject = new Derived;
pDerivedObject->**Base1::**CommonFunction();Does this solve your problem? Or is it something else? Regards, Jijo.
_____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.
-
What is the solution of the diamond problem of Inheriting Classes. A class is inheritting from 2 base classes and both the base classes have a function/method with the same name. From the object of the child class how we can call a function of the one base class.
-
What is the solution of the diamond problem of Inheriting Classes. A class is inheritting from 2 base classes and both the base classes have a function/method with the same name. From the object of the child class how we can call a function of the one base class.
scope resolution operator [::]
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
Never mind - my own stupidity is the source of every "problem" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You/codeProject$$>