我是一个总菜鸟。
我已经创建了int到string和string到int转换的函数。
我想保存它们,以便我可以在任何程序中使用它们,所以我可以像#include <iostream>
我是否通过创建一个类(当时没有私有成员变量?)来实现这一点。
如果我把它作为一个类,如何在不创建对象的情况下使用函数?
基本上我想创建自己的cmath或字符串类型的东西,但我甚至不知道该怎么称呼它来找出如何制作它。
答案 0 :(得分:1)
如果您只有简单的函数,可以将它们放在命名空间中,它也像容器一样,然后将它们放在一个单独的cpp文件中,并创建一个包含原型的.h
文件。
i.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链接到您的项目。
答案 1 :(得分:-1)
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
};
答案 2 :(得分:-3)
#include <iostream>
class Tools {
public :
void static sneeze ();
};
void Tools::sneeze ()
{
std::cout << "atchoum";
}
int main () {
Tools::sneeze(); // atchoum
}