mercredi 20 juin 2018

Why different conversion functions for int and const int are allowed?

Why following is allowed to be compiled in C++:

#include<iostream>
using namespace std;

class mytest
{
public:    
    operator int()
    {
        return 10;
    }

    operator  const int()
    {
        return 5;
    }
};

int main()
{
    mytest mt;
    //int x = mt;    //ERROR ambigious 
    //const int x = mt; //ERROR ambigious
}

why does it make sense to allow different version(based on constness) of conversion operator (to be compiled) when their use always result in ambiguity;

Can someone clarify what I am missing here ?

Aucun commentaire:

Enregistrer un commentaire