|
-
May 30th, 2009, 03:52 AM
#1
headers and static lib: unwanted memory shift ?
Hello,
When I compile my static lib I use a full header (with everything needed).
Then, I made a lite header (without private members and unwanted functions, not needed for the use of the static lib in another project).
But I feel like it's causing some trouble: memory seems to shift on some values. Some variables are fine, others have completely weird values (which doesn't happen when using the full header).
How can I solve this without having to include the full headers (which contain functionnalities I don't want to distribute, and implies lot of other dependencies) ?
Thanks
-
May 30th, 2009, 04:45 AM
#2
Re: headers and static lib: unwanted memory shift ?
You should keep the header file the same, otherwise the compiler will think that the size of your class is smaller than actual.
If you want to hide some details in the your class from others, you can put those details in another class and declare the instance as a private member.
Code:
// In A.h
class B; // Forward declaration
class A
{
public:
A(int i=0);
~A();
void foo();
private:
B *b;
};
// In B.h
class B
{
public:
B(int i);
void bar();
private:
int i;
};
// In A.cpp
#include "A.h"
#include "B.h"
A::A(int i):b(new B(i))
{
}
A::~A()
{
delete b;
}
void A::foo()
{
b->bar();
}
// In B.cpp
#include "B.h"
#include <iostream>
using namespace std;
B::B(int i):i(i)
{
}
void B::bar()
{
cout << i << endl;
}
//In main.cpp
#include "A..h"
int main(void)
{
A a(10);
a.foo();
}
quoted from C++ Coding Standards:
KISS (Keep It Simple Software):
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
Avoid magic number:
Programming isn't magic, so don't incant it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|