简体   繁体   English

警告C4172返回本地变量或临时地址。 但是我返回一个输入参数值

[英]Warning C4172 returning address of local variable or temporary. But I return a input argument value

I designed a template class which can operate several kinds of strings, such as CString and std::string. 我设计了一个模板类,它可以操作多种字符串,例如CString和std :: string。 I have been using it long time, and it works fine. 我已经使用了很长时间,而且效果很好。 But the "return str[i]" in "at" function led to a warning C4172, that means I return an address of local varible. 但是“ at”函数中的“ return str [i]”导致警告C4172,这意味着我返回了本地变量的地址。 But the str isn't a local varible. 但是str不是局部变量。 It should be passed from the outside. 它应该从外面传递。 How can I make the compile not prompt the warning? 如何使编译器不提示警告?

template<class TSTR, typename TCHR>
    class stringOpr
    {
    public:
        typedef typename TSTR TSTRING;
        typedef typename TCHR TCHARTYPE;
        virtual const TCHARTYPE& at(const TSTRING& str, int i) const
        {
            return str[i];//Warning C4172
        }
...
};

在此处输入图片说明

That warning makes sense. 该警告是有道理的。 If there is a MyString that the operator of it didn't return a reference. 如果存在MyString,则其运算符未返回引用。

class MyString
{
char operator[](int i) const;
};

This function will crash. 此功能将崩溃。 In this case, you should rewrite your "at" function to fit a returning reference. 在这种情况下,您应该重写“ at”函数以适合返回的引用。 If you really want to return a reference, do this, 如果您确实要返回参考,请执行此操作,

#pragma warning(disable : 4172)    
virtual const TCHARTYPE& at(const TSTRING& str, int i) const
{
    return str[i];
#pragma warning(default : 4172)
}

Ignore the warning, and re-enable it after this. 忽略警告,然后重新启用它。

/ ---------------------------------------------------- / / ------------------------------------------------- - /

CString::operator[] really doesn't return a reference. CString :: operator []确实不返回引用。 So I make a concession 所以我让步

virtual /*const*/ TCHARTYPE/*&*/ at(const TSTRING& str, int i) const
        {
            return str[i];
        }

暂无
暂无

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

相关问题 返回RValueRef:警告C4172:返回本地变量或临时地址 - Returning RValueRef: warning C4172: returning address of local variable or temporary 返回对 static 成员的引用时出现“警告 C4172:返回局部变量或临时地址” - "warning C4172: returning address of local variable or temporary" when returning reference to static member 这是一个真正的问题吗:警告 C4172:返回局部变量或临时地址 - Is this a real problem: warning C4172: returning address of local variable or temporary 返回指针到局部变量? (警告C4172) - Return pointer to local variable?? (warning C4172) C4172和C4239返回局部变量 - C4172 and C4239 returning local variable 警告 C4172:返回对绑定到局部变量的 const std::string 的引用。 它有多安全? - Warning C4172: Returning a reference to const std::string bound to a local variable. How safe is it? VS2013在返回Rvalue时警告C4172 - VS2013 warning C4172 on return Rvalue 是否有任何情况下C4172 Visual C ++警告不应被视为错误? - Are there any scenarios where C4172 Visual C++ warning should not be considered an error? 为什么返回局部变量的地址或临时只是一个警告而不是错误? - Why is returning address of local variable or temporary only a warning and not an error? 为什么我会通过std :: bind()收到“返回本地临时地址”的警告? - why do I get a 'returning address of local temporary' warning with std::bind()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM