Derived classes, please help

This is a discussion on Derived classes, please help within the C++ Programming forums, part of the General Programming Boards category; How come the following code Code: #include <iostream> using namespace std; class A { int Num; char Word[12]; public: A(int ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    10

    Derived classes, please help

    How come the following code

    Code:
    #include <iostream>
    using namespace std;
    
    class A {
    	int Num;
    	char Word[12];
    
    public:
    	A(int num, char* word) : Num(num) {
    		strncpy(Word, word, 12);
    		cout << "Building A!\n";
    	}
    	void Print()
    	{
    		cout << "Num = " << Num << " Word = " << Word << endl;
    	}
    
    };
    
    class B: private A {
    	float fNum;
    
    public:
    	B(int num, char* word, float f) : A(num, word), fNum(f) {
    		cout << "Building B! \n";
    	}
    
    	void Print() {
    		cout << "I'm B!\n";
    		cout << "fNum = " << fNum << endl;
    		A::Print();
    	}
    };
    
    class C: public B {
    	A myA;
    
    public:
    	C(int num, char* word) : B(num, word, 5.3), myA(num, word) {
    		cout << "Building C!\n";
    	}
    
    	void Print() {
    		cout << "I'm C!\n"; myA.Print(); B::Print();
    	}
    };
    
    void main() {
    	C FunAndAmusement(3, "Seven");
    	FunAndAmusement.Print();
    }
    Gives me the following error:
    Code:
    Error	2	error C2247: 'A' not accessible because 'B' uses 'private' to inherit from 'A'

    It is true that B uses private to inherit from A, but how does this have to do with the fact that i'm using an A data member inside C and using C's constructor init line to initialize it's A datamember?

    EDIT: Is this because B inherits private from A and that includes A's constructor as well, and that's why C cannot use A's constructor since it doesnt have access to it? if so, is there a way to override it and use A as it's datamember? like putting A's constructor in the "protected" or "public" section in class C?
    Thanks.
    Last edited by Mikey_S; 03-01-2009 at 10:47 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    Okay, I am a little stumped by this, but on a hunch I decided to qualify A as being in the global namespace, and the error was fixed, but I am afraid that I cannot really explain why (nor can I explain my intuition).
    Code:
    class C: public B {
    	::A myA;
    
    public:
    	C(int num, char* word) : B(num, word, 5.3), myA(num, word) {
    		cout << "Building C!\n";
    	}
    
    	void Print() {
    		cout << "I'm C!\n"; myA.Print(); B::Print();
    	}
    };
    If I should take a stab at it, my guess would be that the use of the unqualified A in C refers to the A indirect base class sub-object.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    10
    I was thinking about this to.
    If that's the case, how come I cannot fix this by stating A::A inside the "protected" or "public" fields of the B class? that way I can use A's constructor using C because now I'll have access to it ... what do you think?

  4. #4
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    First: int main().

    Second:
    Wow, interesting problem. After a bit of Googling, this article seems to have some info that might be related. Your code will compile if you change:
    Code:
    A myA;
    to:
    Code:
    ::A myA;
    Edit: Wow, posts while I was exploring... :P
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    10
    Thanks alot, that does solve the problem... but how come this issue cannot be solved with "public"ing A::A inside class B (after inheriting "private") anyone has a clue?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    Quote Originally Posted by Mikey_S
    but how come this issue cannot be solved with "public"ing A::A inside class B (after inheriting "private")
    What do you mean by that?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by Mikey_S View Post
    Thanks alot, that does solve the problem... but how come this issue cannot be solved with "public"ing A::A inside class B (after inheriting "private") anyone has a clue?
    Because the problem is not the constructor, it's the class. The class A contains its own name for lookup unqualified lookup. However, qualified lookup of the form A::A cannot find it; it always names the constructor. Thus, you cannot make A::A public in B.

    The unqualified lookup for A in C first searches the bases and finds the A in A, but that is private. So it fails.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. storing derived classes in a stl container
    By *DEAD* in forum C++ Programming
    Replies: 2
    Last Post: 10-03-2008, 07:50 PM
  2. Deleting derived classes
    By *DEAD* in forum C++ Programming
    Replies: 6
    Last Post: 09-30-2008, 12:37 PM
  3. Help accessing classes and derived classes
    By hobbes67 in forum C++ Programming
    Replies: 8
    Last Post: 07-14-2005, 02:46 PM
  4. Replies: 8
    Last Post: 07-27-2003, 01:52 PM
  5. Inheiritance and derived classes
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2001, 02:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21