为什么我不能从const String&方法返回NULL?

MrD*_*Duk 0 c++ const

我有以下方法声明: const String& MyClass::GetAspect(const String& strKey) const

在这个方法中,我们决定在做一些事情之前做一个空指针检查; 如果这个方法里面的指针是null,我们只想返回null.

但是,我收到以下错误:

myclass.cpp(222) : error C2440: 'return' : cannot convert from 'int' to 'const String &'
        Reason: cannot convert from 'int' to 'const String'
        No constructor could take the source type, or constructor overload resolution was ambiguous
Run Code Online (Sandbox Code Playgroud)

有人能帮我理解吗?是否有一些我不完全理解的const-correctness概念?

unw*_*ind 5

NULL不是类型的对象const String,所以当引用时,你当然不能返回它const String.事实上,参考文献的一个主要优点是它们不能(永远)NULL.

重新定义要返回的函数const String *,或返回空String.