Thread: Question about accessing private data when I shouldn't.

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    43

    Question about accessing private data when I shouldn't.

    In the following code, I want main() to have access to some data that it doesn't (and shouldn't) have access to:
    Code:
    #include <vector>
    using namespace std;
    
    template <typename T>
    class A
    {
    private:
    	class B
    	{
    	public:
    		B() { count = 0 }
    		T data;
    	private:
    		unsigned int count;
    		friend class A;
    	};
    public:
    	A();
    	B* do_stuff(T some_data);
    };
    
    template <typename T>
    typename A<T>::B* A<T>::do_stuff(T some_data)
    {
    	B* temp = new B;
    	temp->data = some_data;
    	++(temp->count);
    	return temp;
    }
    
    int main()
    {
    	A<int> MyClass;
    	vector<A<int>::B*> my_vector;
    	for(int i = 0; i < 10; ++i)
    	{
    		my_vector.push_back(MyClass.do_stuff(i));
    	}
    	return 0;
    }
    So, in main() there's a call to A::do_stuff(), which returns a pointer to B. The problem is that I want to store these pointers in a vector, but I can't make a vector of B* in main(), because it's not public. I don't want the whole world to have access to B, and I don't need to modify B directly (other than by the "correct" way) - I just need to store some pointers. What's the right way to go about doing this without breaking things?

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Make a public function that has access to B, and does what you want. That way you can access B and do what you want, but the whole world can't access B and do what they want.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Make B public, but make everything INSIDE B private, with A being a friend class. Now you can pass around pointers to B as much as you want, but only A can do anything with them.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Registered User
    Join Date
    Jun 2008
    Posts
    62
    If you want Private data to be private, then you mark it private. By trying to access data in the main function outside the class you really don't want your data to be private but public. If you don't treat your data like it is private why should anyone else?

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    43
    Quote Originally Posted by brewbuck View Post
    Make B public, but make everything INSIDE B private, with A being a friend class. Now you can pass around pointers to B as much as you want, but only A can do anything with them.
    After reading this, then looking at my code, I realized how wrong my original design was. It's great how a tiny change in perspective can make everything fall into place. Thanks for the insight.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. C# and SQL
    By siten0308 in forum C# Programming
    Replies: 2
    Last Post: 07-09-2008, 12:34 PM
  4. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  5. Replies: 26
    Last Post: 06-15-2005, 02:38 PM