C++ Coding Tips - Chapter1

类别:编程语言 点击:0 评论:0 推荐:

C++ Coding Tips

Betta Jin
<[email protected]>

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.



本文地址:http://com.8s8s.com/it/it29669.htm