相关疑难解决方法(0)

C++:临时论证的生命周期?

当创建a的新实例MyClass作为函数的参数时,如下所示:

class MyClass
{
  MyClass(int a);
};    

myFunction(MyClass(42));
Run Code Online (Sandbox Code Playgroud)

该标准是否使得任何被授权者都能获得析构函数的时间安排?
具体来说,我可以假设在调用之后的下一个语句之前调用它myFunction()吗?

c++ destructor

65
推荐指数
3
解决办法
1万
查看次数

C++ getter应该返回什么?

C++ getter方法的最佳实践是什么,它应该返回一个非平凡类型,但是类型为class或struct的成员.

  1. 按值返回,例如: MyType MyClass::getMyType() { return mMyType; }
  2. 通过const引用返回: const MyType& MyClass::getMyType() { return mMyType; }
  3. 按地址返回: 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)

c++ getter

45
推荐指数
2
解决办法
2万
查看次数

在getter函数中返回const引用或副本?

默认情况下,从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)关于悬挂引用更安全,但对象可能会过时或永远不会存储引用.

当你写一个课程时,你的默认值是什么,而且还不知道(但)性能和生命周期问题是否重要?

附加问题:当成员不是普通字符串而是向量时,游戏会改变吗?

c++ const return-value

41
推荐指数
4
解决办法
2万
查看次数

访问者应该返回值还是常量引用?

假设我有一个Foostd::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++更惯用?

c++ reference accessor return-value

16
推荐指数
3
解决办法
8640
查看次数

12
推荐指数
3
解决办法
2万
查看次数

奇怪的const char*行为

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++ const char

3
推荐指数
1
解决办法
115
查看次数

成员函数的“this”参数的类型为“const”,但我的函数实际上不是“const”

我有一个 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++ dictionary vector stdmap c++11

3
推荐指数
1
解决办法
3849
查看次数