tags:

views:

130

answers:

5

Hi, I have created a big C++ class. I need to give it to another person. I dont want him to see the function implementations but he should be able to use it as a class. (for example: he can inherit this class, use its full functionality in his code but can not see or change function implementations etc)

How can i do this. thanking you..

A: 

Put only the full class declaration in a header, and leave the definitions in a .cpp file, or if you're really srs about it, compile the class as a .lib.

DeadMG
A: 

Create a shared or static library from your sources and distribute header files for others to use your class. Whoever is using your class has to link against your library.

kumar
+8  A: 

Your code is very unlikely to be worth keeping secret. Some C++ code, such as templates and functions that you want inlined in the users code, must be put in the header file. If you really want a measure of secrecy, you can put the implementations of non-template functions in a .cpp file, compile to a library, and then distribute the header and the library. The user can still examine the machine code of the library, of course.

anon
+1 for secrecy being a waste of everyone's time (and tying the hands of whatever programmers get stuck trying to understand or fix bugs in your classes). Plus compiling the C++ code into a library ties it to whatever specific platforms you build for. So if you must, use a code obfuscator on your .cpp files thus they may be recompiled (except, once again, this secrecy will waste your time and that of others): http://en.wikipedia.org/wiki/Obfuscated_code
Hostile Fork
+5  A: 

In C++, a class is just that: a syntactical construct. You are free to arranges your classes across files as you want. You can put each class into it's own header/.cpp file pair, make header-only classes, spread them across many files - as you like.

In C++, you don't distribute classes, you distribute files. For someone to make use of your code, they would need all the declarations (of classes and everything else) for the compiler plus the definitions matching these declarations for the linker. (For what is a declaration and what is a definition see this answer).

Declarations are conventionally put into header files, in C++ usually with a .h or .hpp ending. (There are more conventions, but these are the ones I have seen most.)
Definitions can be distributed either in source (.cpp files) or in their compiled form as object files. If more than one object file is needed, they are often assembled in a library (on Unix-like systems usually with an .a ending, on Windows .lib).

So these are the hard facts:

  1. You cannot avoid distributing header files. Period.
  2. If you don't want others to see your definitions, you will need to compile them and distribute object files, preferably as a libary.

    But distributing libraries definitely has its drawbacks. C++ doesn't define a binary API for compiled code, so code must always be compiled with the same compiler, the same compiler version, and the same compiler settings in order to be linked together.
    That means you either must dictate your costumers their compiler settings (which is hard for them to accept if they have libraries from different vendors with - which is likely - conflicting settings) or you must cater for all their needs (which brings you into trouble).

    Also, since everything in the headers is open to everyone's eyes, inline functions' implementations are, by their very nature, unprotectable. But why use C++, when not using the performance gains they offer?

In the end, distributing source files is often easier. No offence meant, but as Neil put it: If you don't yet know the difference between classes and files and how to distribute them, it is very unlikely your giving away a fortune.

sbi
A: 

The closest to what you want would be to create a .dll for windows or .so for linux and a lib file to go with that and supply that to the person in question.

inquam
You will need the header files as well.
Cthutu