How to hide your source code?

I

Immortal Nephi

How can you hide your implementation source code? You do not want
other programmers to know your source code, but they can only include
interface header. They do not require to know private_foo function.
They only need to use public_foo function when they are ready to link
it to private_foo function’s object code.
Is my example below correct? Please comment your suggestion. The
class keyword can be used in the implementation source code. How do
you say protected or private keyword are not necessary?

// Implemenation.h
int private_foo( int x, int y );


// Implemenation.cpp
#include "impemenation.h"
int private_foo( int x, int y )
{
int z;

for( int a = 0; a < 10; a++ )
z += ( x * y );

return z;
}


// Interface.h
#include "impemenation.h"
int public_foo( int x , y )
{
return private_foo( x , y );
}


// main.cpp
#include "interface.h"

int main()
{
int x = 5;
int y = 10;
int z;

z = public_foo( x , y );

return 0;
}
 
M

Marcel Müller

Hi!

Immortal said:
Is my example below correct?

The proxy function public_foo is superfluous. The header file is by
definition the public part. While the .cpp file is private. See below.
The
class keyword can be used in the implementation source code. How do
you say protected or private keyword are not necessary?

Your example does not use any classes.

In case of classes the things become a bit more complicated because you
have to place at least the class declaration in the public header file.
Search for "PIMPL" (Private IMPLementation idiom) and you will find lots
of information.


Marcel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,414
Latest member
GayleWedel

Latest Threads

Top