?
Solved

Hiding library private members.

Posted on 2009-07-10
8
Medium Priority
?
940 Views
Last Modified: 2012-05-07
I want to hide the private members of my (C++) library that I am creating. I have something like sub-class1, that is a private member of class1. Class1 obviously contains the header for sub-class1. When I compile the code into a library I want other developers to only see class1, but not bee able to see sub-class1 since it's private. Certainly I do not want other developers to be able to see the members of the private sub-class1 object. What methods are available to achieve this?

Thanks,
Matthew
0
Comment
Question by:mattososky
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24824346
Perhaps try an inner class?

class Outer {
   class Hidden {

   };

};
0
 
LVL 31

Assisted Solution

by:Zoppo
Zoppo earned 800 total points
ID: 24824399
Hi mattososky,

a common technique to do this is to implement the 'outer' class as some kind of interface, which calls function in a 'hidden' class. Here's a link to a Wikipedia-article about this technique: http://en.wikipedia.org/wiki/Opaque_pointer 

There you implement a pointer to an unknown class, i.e. like this:

class Class1Impl; // forward declaration
class Class1
{
  Class1Impl* m_pImpl;
public:
  Class1()
  : m_pImpl( new Class1Impl )
  {}

  void foo() { m_pImpl->foo(); }
};

The Class1Impl is completeley implemented in the .cpp file.


Hope that helps,

ZOPPO

0
 
LVL 31

Expert Comment

by:Zoppo
ID: 24824421
Ah, sorry, that was wrong - the 'foo' can only be declared in the header, but not implemented, so it has to be like this:

// in header
...
void foo();
...

// in cpp
...
void Class1::foo()
{
 m_pImpl->foo();
}


Sorry ...

ZOPPO
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 40

Expert Comment

by:evilrix
ID: 24824628
Google for the 'pimpl idiom' which is how you hide implementation in C++
0
 

Author Comment

by:mattososky
ID: 24825442
The idea of a nested class is interesting, but i don't think it's going to cover my problem. Again, I have a library containing classes that are already defined. I don't want to mess with moving their definitions into some kind of out class. I'm not seeing how to incorporate these library classes into an out class.

However, using the opaque point reference, I found this,
http://www.qnx.com/developers/articles/article_302_2.html

That looks alot more what I want to do, but alas at first glance I don't quite understand the implementation. Essentialy what I believe it is saying to to create an interface. But I'm not sure where the interface is created, on the application side or the library side. Fundementally my problem remains the same. There is going to be some kinda of Application->Interface->Library relationship, but I'm not sure what it should look like. Can someone code it quickly? If not, I'll keep toying with it until i get it.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 24825489
I'm home now... the pimpl idiom...

http://www.devx.com/cplus/Article/28105
http://www.gotw.ca/gotw/028.htm
http://www.ddj.com/cpp/205918714

Actually, just realized by Zoppo eluded to this with his post about the opaque pointer... but there is far more to the pimpl idiom than just that so it's worth reading up on it.
0
 

Author Comment

by:mattososky
ID: 24826925
I think I have it. Here is my example. I'm not sure if it follows this pimpl idiom or not, but the key was to put the include for the library header in the code section of the interface. Then have a void pointer in the interface class which be casted into the library class whenever it needs to be accesses. Now the application can include the header for the interface, but that does not allow it to resovle whatever was in the library. Anyone care to comment on this?
//LIBRARY HEADER
////////////////
#include <iostream>
 
class ClassA
{
	public:
 
		int constructor_number;
		ClassA();
		int MySecretFunction();
 
	private:
 
};
 
 
 
//LIBRARY CODE
//////////////
#include "ClassA.h"
 
ClassA::ClassA()
{
	constructor_number = 7;
}
int ClassA::MySecretFunction()
{
	return constructor_number + 1;
}
 
 
 
 
 
 
//INTERFACE HEADER
#include <iostream>
 
typedef void*	PTR_ClassA;
 
class ClassAInterface
{
	public:
 
		PTR_ClassA gp_ClassA;
		ClassAInterface();
		int Function1()
 
};
 
 
 
//INTERFACE CODE
#include "ClassA.h"
 
ClassAInterface::ClassAInterface()
{
	gp_ClassA = new ClassA();
 
}
int ClassAInterface::Function1()
{
	ClassA* true_ptr = (ClassA*)gp_ClassA;
 
	int x = true_ptr->MySecretFunction();
 
	return x;
}

Open in new window

0
 
LVL 40

Accepted Solution

by:
evilrix earned 1200 total points
ID: 24827856
>> Then have a void pointer in the interface class which be casted into the library class whenever it needs to be accesses
Why do you need a void pointer? You can just forward declare the real type of the pointer in the header and then define a pointer of that type.

// HEADER
class ClassA; // This is a forward declaration and from this point on it can be used to define pointers and references

class ClassAInterface
{
ClassA * m_pClassA; // Valid
ClassA & m_rClassA; // Valid
}

>> Now the application can include the header for the interface, but that does not allow it to resovle whatever was in the library. Anyone care to comment on this?
Linking and compiling are unrelated. You still need to link to the library, the fact you are using a pimpl doesn't change this.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question