为什么类的声明与定义写在h文件而不会造成重复定义
发布日期:2021-05-10 02:08:27 浏览次数:13 分类:精选文章

本文共 3083 字,大约阅读时间需要 10 分钟。

C++������������������������������������������

���C++������������������������������������������������������������������������������������������������������������������������������������

1. ���������������������������������

���������������C++������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������ A ���������������������������������������������������������������

class A {
public:
int a;
int b;
}

������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������


2. ������������������������������������������

������������������������������������.cpp������������������������class A������������������������������������������������������������������������������������������������������������obj������������������������������

���������

#include 
using namespace std;
class A {
public:
int a;
int b;
}
class A {
public:
int a;
}
int main() {
A obj;
obj.a; // ��������������������������������������� A
return 0;
}

���������������������������������������������������������������������������


3. ������������������������������������������

C++������������������������������������������������.cpp���������������������������������������������������������������������������������������������������������������������������������������������������������������������

���������

// main.cpp
#include "test.h"
#include
using namespace std;
int main() {
B b;
b.fun(2);
return 0;
}
// test.h
#ifndef _TEST_H
#define _TEST_H
#include
using namespace std;
template
class B {
public:
void fun(T obj) {
cout << "void fun(T obj)" << endl;
cout << obj << endl;
}
#endif

������������������������������������������������������main.cpp���test.h������������������B���������������


4. ���������������������������������������������������������

���C++������������������������������������������������������������������������������������������������������������������������������������������������������������.cpp���.cpp������������������������B���������������������������������������������������������������������������������������

���������������������������������������������������������������������������������������������int a;������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������


������������������������������������

  • ���������������������������������������������������������������������������������������������������������������������������������������
  • ������������������������������������������������������������������������������������������������������������������������������������

������������������������������������������������������������C++���������������������

上一篇:using指定模板类型,以及显示指定模板参数
下一篇:成员函数模板,显示实例化与声明

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2025年04月06日 12时25分10秒