Original Post
I am wondering, why this preprocessor "jungle" doesn't work, it seems to be fine, but linker errors: Its supossed that the compiler, compile .cpp file first, so if USING_INLINE is no defined, it compiles nothing, but i get duplicated names linker error. I am using msvc6 & msvc 2003, althought you think that try to do this is stupid, I want to do it, I need to do it, and I know all the theory of good design of the manual of good c++ programmer, not to inline in excess, inline only the needed, premature optimization etc... If you know where is the fail, or you have a better solution to do the same, please tell me! thanks !!
/********************************************************/
/* Foo.h
/********************************************************/
#ifndef _FOO_H_
#define _FOO_H_
#ifdef USING_INLINE
#define INL __forceinline
#else
#define INL
#endif
class Foo {
private:
int n;
public:
Foo() {};
Foo(int a) { n = a; };
INL int func(int a) const;
INL const Foo& operator = (const Foo& f);
INL const Foo operator + (const Foo& f) const;
};
#ifdef USING_INLINE
#include "Foo.inl"
#endif
#endif // FOO
/********************************************************/
/* Foo.cpp
/********************************************************/
#include "Foo.h"
#ifndef USING_INLINE
int Foo::func(int a) const {
return n*a;
}
const Foo& Foo::operator = (const Foo& f) {
n = f.n;
return *this;
}
const Foo Foo::operator + (const Foo& f) const {
return Foo(n + f.n);
}
#endif
/********************************************************/
/* Foo.inl - basically the same as the .cpp file
/********************************************************/
#ifdef USING_INLINE
int Foo::func(int a) const {
return n*a;
}
const Foo& Foo::operator = (const Foo& f) {
n = f.n;
return *this;
}
const Foo Foo::operator + (const Foo& f) const {
return Foo(n + f.n);
}
#endif
/********************************************************/
/* main.cpp
/********************************************************/
// -- comment or uncomment following line
#define USING_INLINE
#include "Foo.h"
void main() {
Foo f,g;
Foo h(1);
f.func(1);
h = f + g;
// ...
}