static inline

myseemylife 2011-12-09 04:12:52
一个类是所有类的基类,有一个函数。为什么有一个static还有一个inline版的。。。
static是方便调用么?inline加快编译速度么?
static bool connect(const QObject *sender, const char *signal,
const QObject *receiver, const char *member, Qt::ConnectionType =
Qt::AutoConnection);
inline bool connect(const QObject *sender, const char *signal,
const char *member, Qt::ConnectionType type =
Qt::AutoConnection ) const;
...全文
245 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
yisikaipu 2011-12-09
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 myseemylife 的回复:]那这个inline和c语言里面的宏定义是一样的作用咯。。。[/Quote]

是有这么点点意思

在现在的编译器优化面前,inline这个关键字你基本上可以认为是多余的,至少别被它分心

所以这里只要区别静态与非静态

class Cls
{
public:
static void f(int,int)
{
printf("static function\n");
}

void f(int)
{
printf("function\n");
}
};

int main()
{
Cls::f(1,1); // 通过类名直接调用,不需要对象名
//Cls::f(1); // 错误

Cls c;
c.f(1);
c.f(1,1); // 通过对象名也可以调用

return 0;
}
myseemylife 2011-12-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 yisikaipu 的回复:]
根据参数判断这是两个不同的函数
有static的,参考“静态成员函数”
无static的是非静态成员函数

inline是把函数展开为代码,避免函数调用开销,是为了加快运行速度,而不是加快编译速度
[/Quote]
那这个inline和c语言里面的宏定义是一样的作用咯。。。
yisikaipu 2011-12-09
  • 打赏
  • 举报
回复
根据参数判断这是两个不同的函数
有static的,参考“静态成员函数”
无static的是非静态成员函数

inline是把函数展开为代码,避免函数调用开销,是为了加快运行速度,而不是加快编译速度
看到一个不错的c++实现的md5算法 class MD5 { public: typedef unsigned int size_type; // must be 32bit MD5(); MD5(const std::string& text); void update(const unsigned char *buf, size_type length); void update(const char *buf, size_type length); MD5& finalize(); std::string hexdigest() const; friend std::ostream& operator<<(std::ostream&, MD5 md5); private: void init(); typedef unsigned char uint1; // 8bit typedef unsigned int uint4; // 32bit enum {blocksize = 64}; // VC6 won't eat a const static int here void transform(const uint1 block[blocksize]); static void decode(uint4 output[], const uint1 input[], size_type len); static void encode(uint1 output[], const uint4 input[], size_type len); bool finalized; uint1 buffer[blocksize]; // bytes that didn't fit in last 64 byte chunk uint4 count[2]; // 64bit counter for number of bits (lo, hi) uint4 state[4]; // digest so far uint1 digest[16]; // the result // low level logic operations static inline uint4 F(uint4 x, uint4 y, uint4 z); static inline uint4 G(uint4 x, uint4 y, uint4 z); static inline uint4 H(uint4 x, uint4 y, uint4 z); static inline uint4 I(uint4 x, uint4 y, uint4 z); static inline uint4 rotate_left(uint4 x, int n); static inline void FF(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac); static inline void GG(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac); static inline void HH(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac); static inline void II(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac); }; std::string md5(const std::string &str);

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧