当创建a的新实例MyClass
作为函数的参数时,如下所示:
class MyClass
{
MyClass(int a);
};
myFunction(MyClass(42));
Run Code Online (Sandbox Code Playgroud)
该标准是否使得任何被授权者都能获得析构函数的时间安排?
具体来说,我可以假设在调用之后的下一个语句之前调用它myFunction()
吗?
C++ getter方法的最佳实践是什么,它应该返回一个非平凡类型,但是类型为class或struct的成员.
MyType MyClass::getMyType() { return mMyType; }
const MyType& MyClass::getMyType() { return mMyType; }
MyType* MyClass::getMyType() { return &mMyType; }
哪里
class MyType { /* ... */ };
class MyClass
{
private:
MyType mMyType;
}
Run Code Online (Sandbox Code Playgroud)
我特别担心这种方法的以下用法.你能否详细说明这可能会如何影响复制对象,以及悬挂引用和疯狂指针的危险,如果function()
想保存它以供进一步使用.
MyType* savedPointer;
SomeType function(MyType* pointer) { savedPointer = pointer; };
Run Code Online (Sandbox Code Playgroud)
一个.有效期为1和2.
{
MyType t = myClass.getMyType();
function(&t);
}
// is savedPointer still valid here?
Run Code Online (Sandbox Code Playgroud)
湾 有效期为1和2.
{
const MyType& t = myClass.getMyType();
function(&t);
}
// is savedPointer still …
Run Code Online (Sandbox Code Playgroud) 默认情况下,从getter函数返回副本(1)或引用(2)会更好吗?
class foo {
public:
std::string str () { // (1)
return str_;
}
const std::string& str () { // (2)
return str_;
}
private:
std::string str_;
};
Run Code Online (Sandbox Code Playgroud)
我知道2)可能更快,但不必因(N)RVO.1)关于悬挂引用更安全,但对象可能会过时或永远不会存储引用.
当你写一个课程时,你的默认值是什么,而且还不知道(但)性能和生命周期问题是否重要?
附加问题:当成员不是普通字符串而是向量时,游戏会改变吗?
假设我有一个Foo
有std::string
成员的班级str
.应该get_str
返回什么?
std::string Foo::get_str() const
{
return str;
}
Run Code Online (Sandbox Code Playgroud)
要么
const std::string& Foo::get_str() const
{
return str;
}
Run Code Online (Sandbox Code Playgroud)
什么是C++更惯用?
例如
这些中最好的是:
std::string f() {}
Run Code Online (Sandbox Code Playgroud)
要么
const std::string& f() {}
Run Code Online (Sandbox Code Playgroud) GetTypeName是std :: string,代码如下
printf("%#x\n", proto->GetTypeName().c_str());
printf("%s\n", proto->GetTypeName().c_str());
const char *res = proto->GetTypeName().c_str();
printf("%#x\n",res);
printf("%s\n",res);
Run Code Online (Sandbox Code Playgroud)
产生这个输出:
0x90ef78
ValidTypeName
0x90ef78
???????????????????????????????????????????????????????????Q?Z
Run Code Online (Sandbox Code Playgroud)
地址总是一样的; 以下代码(行是交换)
const char *res = proto->GetTypeName().c_str();
printf("%#x\n",res);
printf("%s\n",res);
printf("%#x\n", proto->GetTypeName().c_str());
printf("%s\n", proto->GetTypeName().c_str());
Run Code Online (Sandbox Code Playgroud)
产生这个输出,地址总是不同的:
0x57ef78
Y
0x580850
ValidTypeName
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
strlen(res)
Run Code Online (Sandbox Code Playgroud)
返回无效大小,所以我甚至不能strcpy.
我有一个 C++ std::map
,用于存储有关连接组件的信息。这是我课堂上的代码段BaseStation
,非常基础
//Constructor
BaseStation(string name, int x, int y){
id = name;
xpos = x;
ypos = y;
}
//Accessors
string getName(){
return id;
}
Run Code Online (Sandbox Code Playgroud)
在我的主代码中,我有一个地图声明为
map<BaseStation, vector<string> > connection_map;
在 while 循环中更新connection_map
如下,然后为了我自己的调试目的,我想转储地图的内容。我将 BaseStation 对象附加到地图(作为键),并将 BaseStation 对象的链接列表作为值:
connection_map[BaseStation(station_name, x, y)] = list_of_links;
list_of_links.clear();
for(auto ptr = connection_map.begin(); ptr != connection_map.end(); ++ptr){
cout << ptr->first.getName() << " has the following list: ";
vector<string> list = ptr->second;
for(int i = 0; i < list.size(); i++){
cout …
Run Code Online (Sandbox Code Playgroud) c++ ×7
const ×2
return-value ×2
accessor ×1
c++11 ×1
char ×1
destructor ×1
dictionary ×1
getter ×1
performance ×1
reference ×1
stdmap ×1
vector ×1