LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-19-2002, 08:07 AM   #1
edreddy
Member
 
Registered: Jul 2002
Location: INDIA, Bangalore.
Distribution: Red Hat Enterprise Linux 3.0 Advanced Server
Posts: 34

Rep: Reputation: 15
Talking 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.

Last edited by edreddy; 07-19-2002 at 08:39 AM.
 
Old 07-19-2002, 08:25 AM   #2
orgcandman
Member
 
Registered: May 2002
Location: dracut MA
Distribution: Ubuntu; PNE-LE; LFS (no book)
Posts: 594

Rep: Reputation: 102Reputation: 102
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
 
Old 07-19-2002, 01:55 PM   #3
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,535

Rep: Reputation: 148Reputation: 148
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.
 
Old 07-19-2002, 05:38 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,964
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
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 :} ]
 
Old 07-19-2002, 11:47 PM   #5
Malicious
Member
 
Registered: Jan 2002
Location: Galveston Island
Distribution: suse, redhat
Posts: 208

Rep: Reputation: 30
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(); }
};
 
Old 07-20-2002, 05:25 AM   #6
edreddy
Member
 
Registered: Jul 2002
Location: INDIA, Bangalore.
Distribution: Red Hat Enterprise Linux 3.0 Advanced Server
Posts: 34

Original Poster
Rep: Reputation: 15
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
 
Old 07-31-2002, 06:33 PM   #7
concoran
Member
 
Registered: Jun 2001
Location: 28N,82W
Distribution: XP,Ubuntu 9
Posts: 468

Rep: Reputation: 30
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Implementing a vector class from a list class purefan Programming 9 04-14-2005 10:48 PM
C++ - throwing exceptions in derived class constructors? MadCactus Programming 4 08-09-2004 06:29 AM
BlackBox.class & VerifierBug.class virus ??? dalek Linux - Security 4 02-29-2004 08:55 AM
Inheriting class members (Qt C++, QApplication class) jtshaw Programming 2 01-15-2004 11:52 AM
Communicating Class A and Class C Networks chadtce Linux - Networking 10 07-23-2003 01:36 PM


All times are GMT -5. The time now is 12:38 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration