c++ 杂记

  1. 为什么返回*this?
  1. 友元函数的使用:需要头文件中类内外声明,cpp文件中实现定义哦
cpp 复制代码
// Sales_data.h
#ifndef SALES_DATA_H
#define SALES_DATA_H

#include <string>

class Sales_data {
    std::string bookNo;
    int units_sold = 0;
    double revenue = 0.0;

public:
    // 只声明,不定义
    Sales_data& combine(const Sales_data&);

    // 友元函数:仅声明(friend 关键字 + 函数签名)
    friend Sales_data add(const Sales_data& lhs, const Sales_data& rhs);
    friend std::ostream& print(std::ostream& os, const Sales_data& item);
};

// 类外函数声明(必须!)
Sales_data add(const Sales_data& lhs, const Sales_data& rhs);
std::ostream& print(std::ostream& os, const Sales_data& item);

#endif

4.template

模板的定义必须放在头文件中,不是因为"被其他类使用",而是因为:

编译器在实例化模板时,必须"看到"完整的定义(而不仅仅是声明)

✅ 这与普通函数/类的"声明-定义分离"模型完全不同


🧩 一、普通函数 vs 模板函数:关键区别

❌ 普通函数:可以分离声明与定义

复制代码
// utils.h
void print(int x);  // 声明

// utils.cpp
void print(int x) {         // 定义
    std::cout << x << "\n";
}
  • 编译 main.cpp 时,只需要知道 print 存在(有声明)
  • 链接时,链接器找到 print 的定义

✅ 成功!


❌ 模板函数:不能这样分离!

复制代码
// utils.h
template<typename T>
void print(const T& x);  // 只有声明

// utils.cpp
template<typename T>
void print(const T& x) {        // 定义
    std::cout << x << "\n";
}

// main.cpp
#include "utils.h"
print(42);        // ❌ 编译错误!
print("hello");   // ❌ 编译错误!

为什么?

  • 编译器看到 print(42),需要生成 print<int>(int)
  • 但它只看到了声明,没看到定义
  • 所以无法生成具体代码
  • 链接时也找不到 print<int>,报 undefined reference

👉 模板不是函数,它是一个"生成函数的蓝图"


✅ 正确做法:定义放在头文件中

复制代码
// utils.h
#ifndef UTILS_H
#define UTILS_H

#include <iostream>

// 定义在头文件中
template<typename T>
void print(const T& x) {
    std::cout << x << "\n";
}

#endif

// main.cpp
#include "utils.h"  // 包含了完整定义
print(42);        // ✅ 编译器看到定义,可以实例化 print<int>
print("hello");   // ✅ 实例化 print<const char*>

🧠 二、为什么必须"看到定义"?

因为模板实例化发生在 编译期(compile time),而不是链接期。

编译器要做:

  1. 看到 print(42)
  2. 推导出 T = int
  3. 生成 函数代码:void print<int>(const int&)
  4. 内联或调用

👉 这个过程需要完整的函数体(定义),而不仅仅是签名。

相关推荐
肆忆_1 天前
# 用 5 个问题学懂 C++ 虚函数(入门级)
c++
不想写代码的星星1 天前
虚函数表:C++ 多态背后的那个男人
c++
端平入洛3 天前
delete又未完全delete
c++
端平入洛4 天前
auto有时不auto
c++
郑州光合科技余经理5 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1235 天前
matlab画图工具
开发语言·matlab
dustcell.5 天前
haproxy七层代理
java·开发语言·前端
norlan_jame5 天前
C-PHY与D-PHY差异
c语言·开发语言
哇哈哈20215 天前
信号量和信号
linux·c++
多恩Stone5 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc