C++ Coding Tips
April 15, 2002
Chapter1. Coding Styles
Section1. Let global function body in header file
The 'inline' keyword:
inline void Hello()
{
...
}
- or -
class CHello
{
public:
void SayHello() ;
...
} ;
inline CHello::SayHello()
{
...
}
Note: This style let the compiler know that the function should be only once
instance ingnoring whether the function can indeed to be inlined, and
make the linker happy to link correctly.
The 'friend static inline' modifier:
// xxx.h
#pragma once
class CHello
{
friend static inline void SayHello(CHello * p)
{
ASSERT(p != 0) ;
std::cout << p->m_val ;
...
}
...
protected:
int m_val ;
} ;
// main.cpp
#include "xxx.h"
int main()
{
CHello oHello ;
SayHello(&oHello) ;
return 0 ;
}
Note: Any function modified by friend is global.