开发者

What are the default return values for operator< and operator[] in C++ (Visual Studio 6)?

开发者 https://www.devze.com 2022-12-20 03:14 出处:网络
I\'ve inherited a large Visual Studio 6 C++ project that needs to be translated for VS2005. Some of the classes defined operator< and operator[], but don\'t specify return types in the declarations

I've inherited a large Visual Studio 6 C++ project that needs to be translated for VS2005. Some of the classes defined operator< and operator[], but don't specify return types in the declarations. VS6 allows this, but not VS2005.

I am aware that the C standard specifies that the default return type for normal functions is int, and I assumed VS6 might have been following that, but would this apply to C++ operators as well? Or could VS6 figure out the return type on its own?

For example, the code defines a custom string class like this:

class String {
  char arr[16];
  public:
    operator<(const String& other) { return something1 < something2; }
    operator[](int index) { return arr[index]; }
};

Would VS6 have simply put the return types for both as int, or would it have been smart enough to figure out that operator[] should return a char and operator< should return a bool (and not convert both results to int all the time)?

Of course I have to add return types to make this code VS2005 C++ compliant, but I want to make sure to speci开发者_如何学Pythonfy the same type as before, as to not immediately change program behavior (we're going for compatibility at the moment; we'll standardize things later).


operator< returns a bool by default.

operator[] returns int by default (I think), but it should almost certainly be changed to return whatever the collection contains. For the String example you gave above, that would be a char or wchar_t.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号