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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
07-19-2002, 08:07 AM
|
#1
|
|
Member
Registered: Jul 2002
Location: INDIA, Bangalore.
Distribution: Red Hat Enterprise Linux 3.0 Advanced Server
Posts: 34
Rep:
|
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.
|
|
|
|
07-19-2002, 08:25 AM
|
#2
|
|
Member
Registered: May 2002
Location: dracut MA
Distribution: Ubuntu; PNE-LE; LFS (no book)
Posts: 593
Rep: 
|
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
|
|
|
|
07-19-2002, 01:55 PM
|
#3
|
|
Moderator
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,471
Rep: 
|
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.
|
|
|
|
07-19-2002, 05:38 PM
|
#4
|
|
Moderator
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,903
|
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 :} ]
|
|
|
|
07-19-2002, 11:47 PM
|
#5
|
|
Member
Registered: Jan 2002
Location: Galveston Island
Distribution: suse, redhat
Posts: 208
Rep:
|
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(); }
};
|
|
|
|
07-20-2002, 05:25 AM
|
#6
|
|
Member
Registered: Jul 2002
Location: INDIA, Bangalore.
Distribution: Red Hat Enterprise Linux 3.0 Advanced Server
Posts: 34
Original Poster
Rep:
|
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
|
|
|
|
07-31-2002, 06:33 PM
|
#7
|
|
Member
Registered: Jun 2001
Location: 28N,82W
Distribution: XP,Ubuntu 9
Posts: 468
Rep:
|
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
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 10:23 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|