2

Suppose I have 2 files in my project: "code.h" and "readData.cpp". The code.h file has my class interface (and some of the member function definitions), and the readData.cpp file has the definition of one of my class's private member functions.

Suppose this is the contents of my code.h file:

Class MyClass {
public:
    MyClass() { readData() };
    double sample();

private:
    std::vector<double> data;
    void readData();
    void foo();
};

double MyClass::sample() {
    // ...
}

void MyClass::foo() {
    // ...
}

Suppose this is the contents of my readData.cpp file:

#include "code.h"

void MyClass::readData() {
    data.push_back(1.3083);
    data.push_back(83.24);
    // ...
}

Now, after compiling and producing the object file "readData.o", I create a library using the code:

ar rvs myLib.a readData.o

OK, so far so good. But here's where my question comes. I would like to distribute this myLib.a library and only permit other users to see something like the following interface:

Class MyClass {
    double sample();
};

How can I do that?

synaptik
  • 8,971
  • 16
  • 71
  • 98
  • The pimpl idiom is one way. – Vaughn Cato Dec 16 '12 at 06:14
  • 1
    Related question: http://stackoverflow.com/q/206272/951890 – Vaughn Cato Dec 16 '12 at 06:16
  • @VaughnCato OK, thanks for that suggestion. Suppose I don't care about hiding the interface, but only want to hide the contents of the file readData.cpp. Then, what do I do? Do I just distribute the myLib.a file along with the file code.h? (I've never created a library before) – synaptik Dec 16 '12 at 06:39
  • 1
    That's right. Anything function definition that isn't inline or templated can go into a cpp file. The cpp files are compiled into .o files, and the .o files are collected together into a .a. The final executable is created by linking to the .a. – Vaughn Cato Dec 16 '12 at 15:16

1 Answers1

0

Thanks to the helpful comments by Vaughn Cato, I fixed this problem using the PIMPL strategy.

synaptik
  • 8,971
  • 16
  • 71
  • 98