简体   繁体   中英

Combining conversion constructor with operator overloading

Why is the conversion constructor not called implicitly for i2?

class NumString
{
    public:
        NumString(const char*  s)
        {
        }

        int operator*( int i)
        {
            return 42;
        }
};


int main(void)
{
    int i1 = (NumString) "string" * 2;  //OK
    int i2 =  "string" * 2;             //ERROR
}

因为编译器不会在不涉及任何用户定义类型的情况下调用用户定义的转换。

The expression "string" * 2 involves just a const char * and an int , why should the compiler consider NumString in any way?

If it worked like you expect, how would the complier be expected to choose the correct conversion if more than one class had a suitable converting constructor?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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