C++ 在头文件中放置函数的位置/方式与C++;源文件
有人问了一个与我类似的问题: 为什么只在C++ 在头文件中放置函数的位置/方式与C++;源文件,c++,class,header-files,C++,Class,Header Files,有人问了一个与我类似的问题: 为什么只在.h文件中声明函数而不声明定义,然后在.cpp文件中写入整个定义?重点是什么?为什么不将整个函数放在头文件中的类中,而不是声明它呢?这似乎是重复和毫无意义的 如何构造一个程序来使用头文件和其他.cpp文件,而不将函数、类和变量堆积到主文件中?我可以得到一个使用.h文件和几个.cpp文件的简单程序示例吗 为什么只在.h文件中声明函数而不声明定义,然后在.cpp文件中写入整个定义?重点是什么?为什么不将整个函数放在头文件中的类中,而不是声明它呢?这似乎是重复
.h
文件中声明函数而不声明定义,然后在.cpp
文件中写入整个定义?重点是什么?为什么不将整个函数放在头文件中的类中,而不是声明它呢?这似乎是重复和毫无意义的
如何构造一个程序来使用头文件和其他.cpp
文件,而不将函数、类和变量堆积到主文件中?我可以得到一个使用.h
文件和几个.cpp
文件的简单程序示例吗
为什么只在.h
文件中声明函数而不声明定义,然后在.cpp
文件中写入整个定义?重点是什么?为什么不将整个函数放在头文件中的类中,而不是声明它呢?这似乎是重复和毫无意义的
修改标题时,必须重新编译通过#include
引用该标题内容的所有代码
如果有一个头包含在许多其他文件中,那么对该头内容的一次更改可能需要相当长的时间来重建整个项目。如果您只处理小项目,那么这似乎并不重要,但可以想象有数千个类的生产应用程序
另一个好处是在编写库时。当您提供库二进制文件供其他人使用时,只需提供头文件。通过这种方式,您可以在用户无权访问的源文件中拥有专有的源代码
最后,通过将内容分离为单独的头文件和源文件,可以更轻松地学习库。很多时候,当使用一个新的API时,我只想浏览一下标题,以了解类及其函数的要点。我不需要查看或解析实现
这并不是说您必须有单独的头文件和源文件。有许多只提供标题的库(例如)为广大读者提供服务。当然,这些文件通常只作为头文件分发,因此不需要二进制文件(静态或动态)
当然,如果您正在创建一个模板类,那么就需要实现
如何构造一个程序来使用头文件和其他
.cpp
文件,而不将函数、类和变量堆积到主文件中?我可以得到一个使用.h
和两个.cpp
文件的简单程序示例吗
这可能超出了本网站的范围,但这里有一个简单的例子
矩形.hpp
#ifndef H__RECTANGLE__H
#define H__RECTANGLE__H
class Rectangle
{
public:
Rectangle(float width = 0.0f, float height = 0.0f);
~Rectangle();
void setWidth(float width) noexcept;
void setHeight(float height) noexcept;
float getArea() const noexcept;
protected:
private:
float m_fWidth;
float m_fHeight;
};
#endif
#include "Rectangle.hpp"
Rectangle::Rectangle(float const width, float const height)
: m_fWidth{ width },
m_fHeight{ height }
{
}
Rectangle::~Rectangle()
{
}
float Rectangle::getArea() const noexcept
{
return m_fWidth * m_fHeight;
}
void Rectangle::setWidth(float const width) noexcept
{
m_fWidth = width;
}
void Rectangle::setHeight(float const height) noexcept
{
m_fHeight = height;
}
#include "Rectangle.hpp"
#include <iostream>
int main()
{
Rectangle rectA{ 5, 5 };
Rectangle rectB{ 3, 2 };
std::cout << "Area of Rectangle A is: " << rectA.getArea() << std::endl;
std::cout << "Area of Rectangle B is: " << rectB.getArea() << std::endl;
return 0;
}
矩形.cpp
#ifndef H__RECTANGLE__H
#define H__RECTANGLE__H
class Rectangle
{
public:
Rectangle(float width = 0.0f, float height = 0.0f);
~Rectangle();
void setWidth(float width) noexcept;
void setHeight(float height) noexcept;
float getArea() const noexcept;
protected:
private:
float m_fWidth;
float m_fHeight;
};
#endif
#include "Rectangle.hpp"
Rectangle::Rectangle(float const width, float const height)
: m_fWidth{ width },
m_fHeight{ height }
{
}
Rectangle::~Rectangle()
{
}
float Rectangle::getArea() const noexcept
{
return m_fWidth * m_fHeight;
}
void Rectangle::setWidth(float const width) noexcept
{
m_fWidth = width;
}
void Rectangle::setHeight(float const height) noexcept
{
m_fHeight = height;
}
#include "Rectangle.hpp"
#include <iostream>
int main()
{
Rectangle rectA{ 5, 5 };
Rectangle rectB{ 3, 2 };
std::cout << "Area of Rectangle A is: " << rectA.getArea() << std::endl;
std::cout << "Area of Rectangle B is: " << rectB.getArea() << std::endl;
return 0;
}
main.cpp
#ifndef H__RECTANGLE__H
#define H__RECTANGLE__H
class Rectangle
{
public:
Rectangle(float width = 0.0f, float height = 0.0f);
~Rectangle();
void setWidth(float width) noexcept;
void setHeight(float height) noexcept;
float getArea() const noexcept;
protected:
private:
float m_fWidth;
float m_fHeight;
};
#endif
#include "Rectangle.hpp"
Rectangle::Rectangle(float const width, float const height)
: m_fWidth{ width },
m_fHeight{ height }
{
}
Rectangle::~Rectangle()
{
}
float Rectangle::getArea() const noexcept
{
return m_fWidth * m_fHeight;
}
void Rectangle::setWidth(float const width) noexcept
{
m_fWidth = width;
}
void Rectangle::setHeight(float const height) noexcept
{
m_fHeight = height;
}
#include "Rectangle.hpp"
#include <iostream>
int main()
{
Rectangle rectA{ 5, 5 };
Rectangle rectB{ 3, 2 };
std::cout << "Area of Rectangle A is: " << rectA.getArea() << std::endl;
std::cout << "Area of Rectangle B is: " << rectB.getArea() << std::endl;
return 0;
}
#包括“Rectangle.hpp”
#包括
int main()
{
矩形矩形{5,5};
矩形矩形矩形b{3,2};
STD:当你在学校里使用操作系统设计和编译器设计类,学习链接器如何工作时,你会倾向于回答你的问题。当类中定义的函数被内联时,现代C++编译器避免了代码膨胀的状态是什么,即使它们不舒服的大吗?它们是否处理了这个难题?xities,还是这只是一个老式的问题?@JonathanLeffler说实话,我没有一个好的答案来回答你关于现代编译器的问题。也许这本身就是一个好问题,看看是否有更专业的人可以参与进来。