C++ Coding Tips - Chapter1

王朝c/c++·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

C++ Coding Tips

Betta Jin

<betajin@hotmail.com>

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.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航