在c++类中保存函数
Saving functions in a class C++
我是个十足的新手。
我已经创建了int到string和string到int转换的函数。
我想保存它们以便在任何程序中使用,我可以将它们命名为#include <iostream>
我是否可以通过创建一个类(然后没有私有成员变量)来实现这一点?
,如果我做它作为一个类,我如何使用函数而不创建对象?
基本上我想创建我自己的cmath或string之类的东西但我甚至不知道该叫什么来找出如何创建它
如果你只有简单的函数,你可以把它们放在一个命名空间中,它也像一个容器,然后把它们放在一个单独的cpp文件中,并创建一个包含原型的.h
文件。
。E for mymath.h:
#ifndef MYMATH_H
#define MYMATH_H
namespace mymath
{
int dosomething(int y);
}
#endif
和mymath.cpp:
#include "mymath.h"
int mymath::dosomething(int y)
{
}
然后,当你想使用它,你包括你的#include "mymath.h"
文件和链接cpp到你的项目。
mystring.hpp
#ifndef MYSTRING_HPP
#define MYSTRING_HPP
#include <string>
namespace n_mystring
{
std::string & IntToString( int Int );
int StringToInt( std::string & String );
}
#endif
mystring.cpp
#include "mystring.hpp"
std::string & n_mystring::IntToString( int Int ) {
//.... implementation
};
int n_mystring::StringToInt( std::string & String ) {
//.... implementation
};
#include <iostream>
class Tools {
public :
void static sneeze ();
};
void Tools::sneeze ()
{
std::cout << "atchoum";
}
int main () {
Tools::sneeze(); // atchoum
}
相关文章:
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- 是否可以保存带有参数的函数指针以供以后使用?
- 在每个运行时将函数保存到相同的地址
- 如何使用 pybind11 将 python 函数保存到静态 c++ 容器中?
- 为什么添加析构函数(甚至是空的)会破坏我的结构,该结构使用 ref 转发和折叠来保存 ref 或值的副本?
- (UE4)在标头中保存lambda函数
- 使用函数指针时,ESP 未在函数调用中正确保存
- 如何将 std::string 作为构造函数参数传递,并将其保存的 C 字符串存储在 void 指针中?
- 将子类实例保存在父类型变量中并通过父变量使用 Child 函数?
- 无法绑定可变参数函数并保存到 std::函数
- 如何在程序后台运行函数(特别是自动保存函数)?QT / C++
- Boost:在单独的加载/保存函数中非侵入性地序列化类
- 在数组C++中保存函数指针
- 静态关键字保存函数返回的值
- 使用std::min_element()时保存函数求值
- 在内部结构的定义中使用保存函数的类成员变量,这些内部结构将用作unordered_map对象的模板参数
- 在c++类中保存函数
- 如何在加载/保存函数中读取文件
- 如何在c++中发送或保存函数,接收或恢复函数并执行它
- 正在类中保存函数指针