简体   繁体   English

重载运算符时返回的堆栈地址 memory []

[英]Address of stack memory returned when overloading operator [ ]

I'm encountering some issues with overloading the [] operator for a problem in class.由于 class 中的问题,我在重载 [] 运算符时遇到了一些问题。 This is the function for the overloading:这是用于过载的 function:

const char* Person::operator[](const char* str)
{
    if (strcmp(str, "name") == 0)
        return reinterpret_cast<const char *>atoi(name);

    if (strcmp(str, "age") == 0)
    {
        char temp[4];
        _itoa(age, temp, 10);
        //cout << temp;
        return temp;
    }
}

The class defition looks like this class 定义看起来像这样

class Person
{
private:
    const char* name;
    unsigned int age;
    double height;
    int gradeNo;
    int grades[10];
public:
    Person();
    Person(const char* name, int age, double height);
    void addGrade(int grade);
    const char* operator [] (const char* str);
    operator int();
};

The problem I'm getting is with the return temp;我遇到的问题是return temp; line from the operator overload function.来自运算符重载 function 的行。 CLion returns the following warning: Address of stack memory associated with local variable 'temp' returned CLion 返回以下警告: Address of stack memory associated with local variable 'temp' returned

Sure enough, when trying to use the operator, the value returned is a memory address.果然,在尝试使用操作符的时候,返回的值是一个memory地址。 How can I go about fixing this?我该如何解决这个问题? Is it related to the return type of the function?是不是和function的返回类型有关?

You are taking an address to a temporary (that is located on the stack), that will leave the returned pointer dangling almost immediately.您正在获取一个临时地址(位于堆栈上),这将使返回的指针几乎立即悬空。

I would very strongly suggest using std::string for strings, do not write C++ just as C with classes.我强烈建议使用std::string作为字符串,不要像 C 那样写 C++ 和类。

Then return by std::string by value here.然后在这里按值通过std::string返回。 Bad C-like alternative is to allocate the string on the heap and return that, preferably as std::unique_ptr at least.糟糕的类似 C 的替代方法是在堆上分配字符串并返回,最好至少作为std::unique_ptr

EDIT after the comment below:在下面的评论后编辑:

Since you are required to convert an integer to string and return the value, you cannot return a temporary variable, the result must outlive the method.由于您需要将 integer 转换为字符串并返回值,因此您不能返回临时变量,结果必须比方法更有效。 There are basically two bad options:基本上有两个不好的选择:

  1. Make temp static, this way the pointer remains valid.使temp static,这样指针保持有效。 Downside is the function is no longer re-entrant.缺点是 function 不再可重入。 This is safer because it won't leak.这更安全,因为它不会泄漏。
const char* foo(int age){
    static char temp[4];
    _itoa(age, temp, 10);
    return temp;
}
  1. Return a heap allocated string.返回一个堆分配的字符串。 Big danger is that you are leaving the user to deallocate it:最大的危险是您让用户释放它:
const char* foo(int age){
    char* temp = new char[256];
    _itoa(age, temp, 10);
    return temp;
}

I believe you also have a typo in your code:我相信您的代码中也有错字:

return reinterpret_cast<const char *>atoi(name);

The atoi should not be there, right? atoi不应该在那里,对吧? reinterpret_cast should not be needed.不需要reinterpret_cast

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM