例如,我们有编码功能.使用的最佳做法是什么:
void Crypto::encoding(string &input, string &output)
{
//encoding string
output = encoded_string;
}
Run Code Online (Sandbox Code Playgroud)
要么
string Crypto::encoding(string &input)
{
//encoding string
return encoded_string;
}
Run Code Online (Sandbox Code Playgroud)
我们应该使用引用还是返回来返回字符串?据我所知,返回一个字符串将需要一些时间来初始化一个将由return指令返回的新字符串.在处理引用的变量时,我不会浪费时间来初始化一些新的变量,我只是结束了这个函数.
我们应该主要使用reference和make函数返回类型void吗?或者,我们应该引用时,我们要返回两个或多个变量,当我们需要返回一个变量,然后使用返回指令只返回的数据?
我有许多小函数,每个函数都做一件事,例如:pingServer,checkUserValidAccount,countDistance.
将每个函数包装到单个类中是不值得的.
在c ++中处理这么多不同的小函数的最佳实践是什么?
也许写一些名为Helpers的类,例如NetworkHelpers?
感觉是auto_ptr
什么?看看这段代码:
#include <iostream>
#include <memory>
class A
{
public:
~A()
{
std::cout << "DEST";
};
};
void func(A* pa)
{
std::cout << "A pointer";
}
void test()
{
A a;
std::auto_ptr<A> pA(new A);
//func(pA); //compiler error here cannot convert parameter 1 from 'std::auto_ptr<_Ty>' to 'A *'
std::cout << "end";
}
int main(int argc, char* argv[])
{
test();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
使用它有auto_ptr
什么意义?
auto_ptr
的A[]
或char[]
因为auto_ptr的电话删除不delete[]
.唯一的想法是我不必编写删除,但是当我超出范围时,如果它将被销毁,那么指针的意义是什么.我使用指针来控制变量的实时.
普通变量初始化是在堆栈上的指针和堆上的指针,但告诉我使用auto_ptr
正常指针的意义是什么?