gpt4 book ai didi

与类相关的 C++ 头文件

转载 作者:太空狗 更新时间:2023-10-29 21:05:56 26 4
gpt4 key购买 nike

我一直在阅读有关头文件的不同文章和教程。我知道 Headers 的目的是使“接口(interface)”不受实现的影响。 (以及其他诸如一些编译优化之类的东西)

我仍然不明白,而且真的无法理解,你总是使用标题吗?我知道您可以在头文件本身中编写代码块。但这就是我迷路的地方。

当我看视频教程时,人们只是在头文件中用函数体定义函数。然后另一篇文章只定义了功能(我猜这是接口(interface)的想法)。

目前我正在创建一个名为 Color 的简单类。实现:

/* 
* File: Color.cpp
* Author: Sidar
*
* Created on 26 december 2011, 16:02
*/

#include <stdio.h>

#include "Color.h"

Color::Color() {

reset();
}

Color::Color(const Color& orig) {

a = orig.a;
r = orig.r;
g = orig.g;
b = orig.b;
}

void Color::reset()
{
a = 0;
r = 0;
g = 0;
b = 0;
}

Color::Color(unsigned int r, unsigned int g, unsigned int b, unsigned int a)
{
this->r = r;
this->g = g;
this->b = b;
this->a = a;
}

Color::~Color() {
r = 0;
g = 0;
b = 0;
}

//getters____________________________
unsigned int Color::getRed() const
{
return r;
}

unsigned int Color::getBlue() const
{
return b;
}

unsigned int Color::getGreen() const
{
return g;
}

unsigned int Color::getAlpha() const
{
return a;
}

//setters____________________________

void Color::setRed(unsigned int r)
{
if(r > 255)r = 255;
if(r < 0)r = 0;

this->r = r;
}


void Color::setGreen(unsigned int g)
{
if(g > 255)g = 255;
if(g < 0)g = 0;

this->g = g;
}

void Color::setBlue(unsigned int b)
{
if(b > 255)b = 255;
if(b < 0)b = 0;

this->b = b;
}

void Color::setAlpha(unsigned int a)
{
if(a > 255)a = 255;
if(a < 0)a = 0;

this->a = a;
}

unsigned int Color::color()
{
return (int)a << 24 | (int)r << 16 | (int)g << 8 | (int)b << 0;
}

这里是标题

/* 
* File: Color.h
* Author: Sidar
*
* Created on 26 december 2011, 16:02
*/

#ifndef COLOR_H
#define COLOR_H
#include <string>

class Color {
public:

Color();
Color(const Color& orig);
Color(unsigned int r,unsigned int g,unsigned int b, unsigned int a);

virtual ~Color();
//____________________
void setRed(unsigned int r);
unsigned int getRed()const;
//____________________
void setBlue(unsigned int b);
unsigned int getBlue()const;
//____________________
void setGreen(unsigned int g);
unsigned int getGreen()const;
//____________________
void setAlpha(unsigned int a);
unsigned int getAlpha()const;
//____________________
unsigned int color();

void reset();

private:

unsigned int r;
unsigned int b;
unsigned int g;
unsigned int a;


};

#endif /* COLOR_H */

此代码确实有效,我没有收到任何错误。但这是头文件和cpp文件的总体思路吗?我的第二个问题:我读了很多,当使用模板时,只在 header 中实现代码会更容易,我理解这一点(以防止对假设如此通用的东西进行许多实现)。但是还有其他情况吗?

最佳答案

你不会“总是”做任何事情,这完全取决于环境、你的目标以及你的团队或组织的编码标准。

C++ 是一种非常灵活的语言,允许以多种不同的方式完成和组织事情。

可能使用单独的实现文件的一些原因:

  1. 保持实现与接口(interface)分离,就像你建议

  2. 加快编译时间

  3. 处理循环依赖

  4. 所以你可以发布一个只包含头文件而不包含头文件的二进制库底层源码

您可能不想要单独的实现文件的一些原因:

  1. 您使用模板,“通常”必须使用声明

  2. 您不希望实现与接口(interface)分离。在很多情况下,这会让事情更容易理解,因为你没有在头文件和实现文件之间来回切换。这个如果你正在处理大型类(class),可能会适得其反不过方法很多。

  3. 您希望尽可能多的代码被编译器内联可能。

  4. 您正在创建一个代码库,您不希望用户访问它不得不担心建筑。大多数 Boost 库都是这样,您就不必使用 Boost 构建系统,可能很麻烦,但您只需包含头文件在您的代码中,这就是您使用它们所需要做的一切。


我通常通过在头文件中定义所有逻辑来开始新类的工作。然后当类完成后,或者当它开始在头文件中变得拥挤时,我将开始将逻辑移出到一个单独的实现文件中。这完全是为了充分利用我的时间,因为当我可以在同一个文件中查看所有内容时,我能够更快地完成工作并减少错误。

还应该注意的是,您根本不必使用头文件。您可以直接在 .cpp 文件中定义一些类。这通常用于永远不会在该 .cpp 文件之外使用的私有(private)类。

关于与类相关的 C++ 头文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8637606/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com