C++类的内联成员函数应放在哪
2013-03-13 15:23
176 查看
今天复习C++ Primer的时候,看到了关于C++类的内联成员函数的放置,应该放在头文件中。那么这到底是为什么呢?仅仅是一种代码规范问题还是必须这样做呢?
下面我就来讲讲我自己的理解吧。要彻底理解这个问题,首先就要了解下函数的声明和定义了。我们知道,函数可以在多处声明,但只能在一个地方定义,不然就会出现重定义。大部分函数默认是外部链接,而inline函数默认为内部链接。也就是说inline函数只能在本文件中使用,对其他文件是不可见的。一般我们使用某个类的时候,都是在文件中加上该类的头文件,以便我们可以使用该类的接口。而我们类的成员函数的实现都是放在相应的.cpp文件中的,而在.h文件中声明。这样我们便可以通过.h文件中的成员函数的声明找到其定义,继而使用成员函数了。但如果将inline函数放在.cpp文件中,那么其只对.cpp文件有效,这样我们就无法访问它了。所以我们将其放在类的声明的头文件中,这样通过包含该头文件来使用它。
下面写个实际的例子来说明一下,我先把内联函数放到类声明的头文件中:
[cpp] view plaincopyprint?
/*test.h*/
#ifndef TEST_H
#define TEST_H
#include <iostream>
using std::cout;
using std::endl;
class test
{
public:
test():x(10){}
inline void print();
void display (int y);
private:
int x;
};
void test::print()
{
cout << x << endl;
}
#endif
[cpp] view plaincopyprint?
/*test.cpp*/
#include <iostream>
#include "test.h"
using std::cout;
using std::endl;
void test::display(int y)
{
cout << x * y << endl;
}
[cpp] view plaincopyprint?
/*main.cpp*/
#include <iostream>
#include "test.h"
using namespace std;
int main()
{
test T;
T.display(10);
T.print();
system("pause");
return 0;
}
运行结果正常,下面来看看将内联函数放到.cpp中去:
[cpp] view plaincopyprint?
/*test.h*/
#ifndef TEST_H
#define TEST_H
#include <iostream>
using std::cout;
using std::endl;
class test
{
public:
test():x(10){}
inline void print();
void display (int y);
private:
int x;
};
#endif
[cpp] view plaincopyprint?
/*test.cpp*/
#include <iostream>
#include "test.h"
using std::cout;
using std::endl;
void test::print()
{
cout << x << endl;
}
void test::display(int y)
{
cout << x * y << endl;
}
测试函数和上面的main.cpp是一样的。这是出现了错误:
error LNK2019: 无法解析的外部符号 "public: void __thiscall test::print(void)" (?print@test@@QAEXXZ),该符号在函
数 _main 中被引用。如果我将测试函数改为:
[cpp] view plaincopyprint?
int main()
{
test T;
T.display(10);
//T.print();
system("pause");
return 0;
}
那么运行结果正常。从此可以得出结论:内联函数放在头文件或者.cpp中都是没有错的,但如果我们需要在程序中访
问它,那么就必须将其放在头文件中。
转自:http://www.cdtarena.com/cpx/201303/7942.html
下面我就来讲讲我自己的理解吧。要彻底理解这个问题,首先就要了解下函数的声明和定义了。我们知道,函数可以在多处声明,但只能在一个地方定义,不然就会出现重定义。大部分函数默认是外部链接,而inline函数默认为内部链接。也就是说inline函数只能在本文件中使用,对其他文件是不可见的。一般我们使用某个类的时候,都是在文件中加上该类的头文件,以便我们可以使用该类的接口。而我们类的成员函数的实现都是放在相应的.cpp文件中的,而在.h文件中声明。这样我们便可以通过.h文件中的成员函数的声明找到其定义,继而使用成员函数了。但如果将inline函数放在.cpp文件中,那么其只对.cpp文件有效,这样我们就无法访问它了。所以我们将其放在类的声明的头文件中,这样通过包含该头文件来使用它。
下面写个实际的例子来说明一下,我先把内联函数放到类声明的头文件中:
[cpp] view plaincopyprint?
/*test.h*/
#ifndef TEST_H
#define TEST_H
#include <iostream>
using std::cout;
using std::endl;
class test
{
public:
test():x(10){}
inline void print();
void display (int y);
private:
int x;
};
void test::print()
{
cout << x << endl;
}
#endif
[cpp] view plaincopyprint?
/*test.cpp*/
#include <iostream>
#include "test.h"
using std::cout;
using std::endl;
void test::display(int y)
{
cout << x * y << endl;
}
[cpp] view plaincopyprint?
/*main.cpp*/
#include <iostream>
#include "test.h"
using namespace std;
int main()
{
test T;
T.display(10);
T.print();
system("pause");
return 0;
}
运行结果正常,下面来看看将内联函数放到.cpp中去:
[cpp] view plaincopyprint?
/*test.h*/
#ifndef TEST_H
#define TEST_H
#include <iostream>
using std::cout;
using std::endl;
class test
{
public:
test():x(10){}
inline void print();
void display (int y);
private:
int x;
};
#endif
[cpp] view plaincopyprint?
/*test.cpp*/
#include <iostream>
#include "test.h"
using std::cout;
using std::endl;
void test::print()
{
cout << x << endl;
}
void test::display(int y)
{
cout << x * y << endl;
}
测试函数和上面的main.cpp是一样的。这是出现了错误:
error LNK2019: 无法解析的外部符号 "public: void __thiscall test::print(void)" (?print@test@@QAEXXZ),该符号在函
数 _main 中被引用。如果我将测试函数改为:
[cpp] view plaincopyprint?
int main()
{
test T;
T.display(10);
//T.print();
system("pause");
return 0;
}
那么运行结果正常。从此可以得出结论:内联函数放在头文件或者.cpp中都是没有错的,但如果我们需要在程序中访
问它,那么就必须将其放在头文件中。
转自:http://www.cdtarena.com/cpx/201303/7942.html
相关文章推荐
- 浅谈C++类的内联成员函数应放在那个文件
- C++类的内联成员函数应放在哪
- C++类的内联成员函数应放在哪
- C 类的内联成员函数应放在哪
- c++类的模板成员函数申明和实现必须放在一起
- C++类 给结构体成员 函数指针 赋值
- C++类静态成员与类静态成员函数
- C++类成员函数转换成函数对象
- 重温C++ primer 之C++类中的static数据成员,static成员函数
- C++学习(三) C++类中成员函数的定义
- C++类对象内存模型与成员函数调用分析(中)
- 模板类成员函数的定义和声明为什么要放在一个文件中
- C++类和对象及其默认成员函数
- 使非MFC窗口程序的窗口回调过程成为C++类的成员函数
- c++类中的静态成员函数总结
- C++类中不写成员函数易犯错模型
- c++类成员函数作为回调函数
- C++类类型成员函数的重载、覆盖与隐藏
- C++类静态成员与类静态成员函数
- C++类模板的成员函数模板写法收藏