Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 04-15-2005, 07:36 AM   #1 (permalink)
lostromos
Registered User
 
Join Date: Apr 2005
Posts: 5
lostromos is on a distinguished road
Multiple Inheritance

Hello all,

Something i thought was simple has bothered my sleep last night. Imagine two classes A,B and a third C that inherits from the other two.

class C : public A, public B

There is an attribute in class A and in class B called n. It's private in A and protected in B. I thought that class C would have only acces to the B::n as the A::n is private. But whan i use the variable n inside class C, the complier finds the variable n ambiguous! Any ideas?

Thanks in advance, have a nice day

George
lostromos is offline   Reply With Quote
Old 04-15-2005, 11:43 AM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Yes, this is the case:
Code:
#include <iostream>
#include <string>

//-
using std::string;
using std::endl;
using std::cout;
using std::cin;
//-

class A
{
public:
  A::A() : theString("A") {}
protected:
  string theString;  
};

class B
{
public:
  B::B() : theString("B") {}
private:
  string theString;  
};

class C : public A , public B
{
public:
  void print()
  {
    cout<<theString<<endl;
  }
 
};

int main()
{
  C theC;
  theC.print();
   
  cin.get();
  return 0;
}
And will error indeed.

Let's see how this looks in memory:
Code:
CLASS A string theString;
CLASS B string theString;
CLASS C void print
As you can see, internally the system doesn't care about access specifiers. Public, private, all the same memory wise. But it does know what member belongs to what class. And here lies our solution: We need to tell the system which one we mean.
Use the scope resolution operator "::"
Code:
class C : public A , public B
{
public:
  void print()
  {
    cout<<A::theString<<endl;
  } 
};
Or formally:
Access specifiers do not play a role in name lookup resolution.

That was another good question.
__________________
Valmont is offline   Reply With Quote
Old 04-15-2005, 02:06 PM   #3 (permalink)
lostromos
Registered User
 
Join Date: Apr 2005
Posts: 5
lostromos is on a distinguished road
Valmont,

Thanks a lot for your thorough answer and for all the explanation. I should guess that what you said is also valid for the private functions of the mother classes A and B.

Best,

George
lostromos is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
NumberFormatException: multiple points sde Java 3 07-27-2004 09:44 AM
Would be really grateful for help with inserting multiple records debbieau PHP 2 06-16-2004 05:06 PM
capturing multiple keys sde MS Technologies ( ASP, VB, C#, .NET ) 0 01-20-2004 03:17 PM
multiple ips in debian sde Linux / BSD / OS X 7 04-01-2003 07:23 PM
what's the best way to use multiple dbs php/mysql sde PHP 1 08-09-2002 10:55 AM


All times are GMT -8. The time now is 08:19 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting