请教一个vector的问题
请教一个vector的问题,看到一段代码是这样定义的
struct STstStr
{
.......
}
std::vector<STstStr>& vec(void) {
static std::vector<queued_canon> tmpVec;
return tmpVec;
}
请问:
1. 这样定义和直接std::vector<STstStr> vec有什么区别?
2. tmpVec在此起何作用
#include <vector> using namespace std; vector<int>& vec() { static vector<int> i(10, 9); return i; } int main() { vector<int> i; i = vec(); return 0; } +++++++++++++++++++++++++++ +++++++++++++++++++++++++++ #include <vector> using namespace std; vector<int>& vec() { vector<int> i(10, 9); return i; } int main() { vector<int> i; i = vec(); return 0; }
#include <iostream> using namespace std; class CTest { public: CTest(){ cout << "CTest()..." << endl; } CTest(const CTest &nTest){ cout << "CTest(const CTest &nTest)..." << endl; } }; void GetInstance(CTest &nTest) { nTest; return; } int main() { CTest nTest; GetInstance(nTest); return 0; } ************************ ************************ #include <iostream> using namespace std; class CTest { public: CTest(){ cout << "CTest()..." << endl; } CTest(const CTest &nTest){ cout << "CTest(const CTest &nTest)..." << endl; } }; void GetInstance(CTest nTest) { nTest; return; } int main() { CTest nTest; GetInstance(nTest); return 0; }