Thread: Hide class implementation from driver?

  1. #1
    Polar Fuzz
    Join Date
    Oct 2003
    Posts
    36

    Hide class implementation from driver?

    Is there a way to hide a class implementation from a driver?

    I want to be able to have a driver file that can instantiate an object from CLASS A, but not be able to instantiate anything or access anything from CLASS B.

    However, I want CLASS A to have private data access to CLASS B?

    Does nesting classes accomplish this?

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    I'm thinking.. make class B a container class.. with pure virtual functions.. this will prevent class B from instantiating any objects...


    I don't think a derived class can access 'private' variables from the parent class..

    so I'm thinkin' Class B will have 'protected' level variables..


    Then you can derive class A : protected class B
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    I'm not exactly sure what you mean... but if you make CLASS B a private nested class in CLASS A, then no other code outside of CLASS A and its friends would be able to instantiate or access CLASS B.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I want to be able to have a driver file that can instantiate an object from CLASS A, but not be able to instantiate anything or access anything from CLASS B.
    Nest class B within class A's private section and make class A a friend of class B to access B's private members from A:
    Code:
    #include <iostream>
    
    class A {
      class B {
        friend class A;
        int i;
      } b;
    public:
      A() { b.i = 10; }
      void print() { std::cout<< b.i <<std::endl; }
    };
    
    int main()
    {
      A a;
    
      a.print();
    }
    My best code is written with the delete key.

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Is making A a friend of B really necessary? Unless there are friends of A that you don't want to see B, you could just use normal encapsulation to give A whatever access to B is appropriate.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Is making A a friend of B really necessary?
    No, but accessing B's private members was a part of the question.
    However, I want CLASS A to have private data access to CLASS B
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  3. Replies: 8
    Last Post: 10-02-2005, 12:27 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM