How to hide your source code?

Discussion in 'C++' started by Immortal Nephi, Aug 10, 2009.

  1. 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;
    }
    Immortal Nephi, Aug 10, 2009
    #1
    1. Advertising

  2. Hi!

    Immortal Nephi wrote:
    > 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

    > // Implemenation.h

    int foo( int x, int y );
    >
    >
    > // Implemenation.cpp
    > #include "implemenation.h"

    int foo( int x, int y )
    > {
    > int z;
    >
    > for( int a = 0; a < 10; a++ )
    > z += ( x * y );
    >
    > return z;
    > }


    > // main.cpp

    #include "implementation.h"
    >
    > int main()
    > {
    > int x = 5;
    > int y = 10;
    > int z;
    >
    > z = public_foo( x , y );
    >
    > return 0;
    > }
    Marcel Müller, Aug 10, 2009
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ross

    hide my source code/

    ross, Jun 27, 2003, in forum: HTML
    Replies:
    17
    Views:
    4,104
    Daniel Doman
    Jun 28, 2003
  2. Replies:
    41
    Views:
    2,299
    Samuel van Laere
    Jun 1, 2007
  3. Your Encryption Site
    Replies:
    11
    Views:
    691
    Kim André Akerø
    Apr 24, 2008
  4. Replies:
    1
    Views:
    367
    Steve C. Orr [MCSD, MVP, CSM, ASP Insider]
    Jun 20, 2008
  5. Replies:
    5
    Views:
    162
    Ferd Berfle
    Sep 7, 2005
Loading...

Share This Page