Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;辅助函数_C++ - Fatal编程技术网

C++ C++;辅助函数

C++ C++;辅助函数,c++,C++,我有很多这样的代码: otherString1 = myString1.replace("a", "b").replace("c", "d").replace("e", "f"); otherString2 = myString2.replace("a", "b").replace("c", "d").replace("e", "f"); otherString3 = myString3.replace("a", "b").replace("c", "d").replace("e", "f");

我有很多这样的代码:

otherString1 = myString1.replace("a", "b").replace("c", "d").replace("e", "f");
otherString2 = myString2.replace("a", "b").replace("c", "d").replace("e", "f");
otherString3 = myString3.replace("a", "b").replace("c", "d").replace("e", "f");
我不想重复那些
replace
方法。什么是重新分解此类代码的正确方法?我是C++的新手…< / P> 我想我可以做到:

#define REPLACE .replace("a", "b").replace("c", "d").replace("e", "f")
otherString1 = myString1#REPLACE;
但这是行不通的

显然,我无法对字符串类进行修补以添加
myReplace()

怎么办?我应该把替换代码放在头文件还是源文件中?那些
静态
内联
常量
的东西呢?我应该创建一个完整的helper类和一个helper方法,还是应该在某个地方创建一个函数?比如说:

[helper.hpp]
static inline const myReplace(const StringClass s);

[helper.cpp]
static inline const myReplace(const StringClass s) {
    return s.replace("a", "b").replace("c", "d").replace("e", "f");
}

[somefile.cpp]
include "helper.hpp"
otherString3 = myReplace(myString3);

依我看,你想得太多了。只需创建一个函数,该函数接受一个字符串(通过
const
reference)并返回修改后的字符串。在标题中声明它并在相应的
.cpp
文件中定义

工作完成了

[helper.hpp]
std::string myReplace(const std::string& s);

[helper.cpp]
std::string myReplace(const std::string& s) {
   ...
}

[somefile.cpp]
#include "helper.hpp"
otherString3 = myReplace(myString3);

依我看,你想得太多了。只需创建一个函数,该函数接受一个字符串(通过
const
reference)并返回修改后的字符串。在标题中声明它并在相应的
.cpp
文件中定义

工作完成了

[helper.hpp]
std::string myReplace(const std::string& s);

[helper.cpp]
std::string myReplace(const std::string& s) {
   ...
}

[somefile.cpp]
#include "helper.hpp"
otherString3 = myReplace(myString3);

我只想指出,你的宏本来可以工作的,你只是用错了。但是,这不是解决这个问题的正确方法,只是想指出一点。以下是正确的用法:

#define REPLACE .replace("a", "b").replace("c", "d").replace("e", "f")
otherString1 = myString1 REPLACE;
或者更好(如果使用宏可以更好):


请记住,不要这样做,但这就是宏的使用方式。

我只想指出,您的宏本来可以工作,但您只是错误地使用了它。但是,这不是解决这个问题的正确方法,只是想指出一点。以下是正确的用法:

#define REPLACE .replace("a", "b").replace("c", "d").replace("e", "f")
otherString1 = myString1 REPLACE;
或者更好(如果使用宏可以更好):



请记住,不要这样做,但这就是宏的使用方式。

StringClass
??你写了自己的字符串类吗?不,这只是一个例子。可能是任何其他方法……TUOML:我不是故意粗鲁,但我感觉一个好的入门级C++书在这个时候是一个不错的投资。@ NP:一点也不粗鲁,你是对的。<代码> StringClass <代码>?你写了自己的字符串类吗?不,这只是一个例子。可能是任何其他方法……TUOML:我不是故意粗鲁,但是我感觉一个好的入门级C++书在这个时候是一个不错的投资。@ NP:根本不粗鲁,你是对的。它应该是一个正常的自由函数,而不是一个方法。什么方法?助手班
const
reference——你是说
const StringClass&s
?一种不存在的方法,换句话说就是一个自由函数。使用
StringClass
而不是
std::string
的原因是什么?是否更好:
inline const std::string myReplace(const std::string&s)
?我的编译器内联函数您没有要求它内联,也拒绝内联函数您确实要求它内联。这与现代C++编译器是常见的。所以这两种方式都没有什么区别。除了内联函数在头文件中,非内联函数在cpp文件中之外。这就是内联的意思。它应该是一个普通的自由函数,而不是一个方法。什么方法?助手班
const
reference——你是说
const StringClass&s
?一种不存在的方法,换句话说就是一个自由函数。使用
StringClass
而不是
std::string
的原因是什么?是否更好:
inline const std::string myReplace(const std::string&s)
?我的编译器内联函数您没有要求它内联,也拒绝内联函数您确实要求它内联。这与现代C++编译器是常见的。所以这两种方式都没有什么区别。除了内联函数在头文件中,非内联函数在cpp文件中之外。这就是内联的意思。好的,谢谢。但是你能解释一下为什么这不是一个好的实践吗?@TuomL因为当你使用宏时,很难判断到底会发生什么。因为它们实际上是你所写内容的文本扩展,所以通常会发生非常糟糕的事情。例如,有关一些好的示例,请参见和。好的,谢谢。但是你能解释一下为什么这不是一个好的实践吗?@TuomL因为当你使用宏时,很难判断到底会发生什么。因为它们实际上是你所写内容的文本扩展,所以通常会发生非常糟糕的事情。例如,有关一些好的示例,请参见和。