首页 > 解决方案 > 什么是 C++ 中的“类 C”模块?以及如何实施?

问题描述

我是 C++ 新手,我有一个任务需要在 C 类模块中完成。根据我的教授的说法,“类 C”模块没有编写任何类,只有一个非成员(类 C)函数。我的任务是在 .h 文件中编写一个带有函数 sort() 的 C 风格模块 sort.h 和 sort.cpp。sort() 接受一个整数数组并使用冒泡排序对数组进行排序。如果不涉及课程,我该如何实现?

标签: c++c

解决方案


在 C++ 语言中,您可以拥有类之外的函数,称为独立函数。

排序.hpp:

#ifndef SORT_HPP
#define SORT_HPP

#include <vector>

void my_sort(std::vector& v);

#endif

排序.cpp:

#include "sort.hpp"

void my_sort(std::vector& v)
{
  // Insert code to sort the vector here.
}

它是一个类 C 模块,因为 C 语言不支持类和成员函数。


推荐阅读