LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   c++ : regarding (inheritence)base class and derived class (https://www.linuxquestions.org/questions/programming-9/c-regarding-inheritence-base-class-and-derived-class-26140/)

edreddy 07-19-2002 08:07 AM

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.

orgcandman 07-19-2002 08:25 AM

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

Mara 07-19-2002 01:55 PM

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.

Tinkster 07-19-2002 05:38 PM

Re: c++ : regarding (inheritence)base class and derived class
 
Quote:

Originally posted by edreddy
hey! is there any one to solve my small conceptual problem:
here is c++ classes :

[some detail removed :}]

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.

Hmmm ... maybe my simplistic approach doesn't
suit your needs ...
Did you consider scope resolution?

class C : public class A , public class B
{
A.function1();
};

Cheers,
Tink [ Puny programmer :} ]

Malicious 07-19-2002 11:47 PM

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(); }
};

edreddy 07-20-2002 05:25 AM

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

concoran 07-31-2002 06:33 PM

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.