Hide class implementation from driver?

This is a discussion on Hide class implementation from driver? within the C++ Programming forums, part of the General Programming Boards category; Is there a way to hide a class implementation from a driver? I want to be able to have a ...

  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,088
    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,796
    >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,088
    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,796
    >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, 11:05 AM
  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, 06:18 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

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