Здравствуйте, Greg Zubankov, Вы писали:
GZ>Здравствуйте, acoder, Вы писали:
A>>Первый пример:
A>>Как я понимаю, логика следующая: S, с помощью user defined conversion, можно превратить в сhar* и в short*. Оба преобразования эквивалентны, следовательно компилятор не может выбрать лучший вариант и выдает ошибку.
GZ>Все верно
Это ещё почемуже?
// test1.cpp
struct S {
operator short* ();
operator char* () const;
};
void f (short*);
void f (char*);
void foo (S& s)
{
f (s);
}
g++ -c test1.cpp
test1.cpp: In function 'void foo(S&)':
test1.cpp:11: error: call of overloaded 'f(S&)' is ambiguous
test1.cpp:6: note: candidates are: void f(short int*)
test1.cpp:7: note: void f(char*)
f(char*) никакой не кондидат, т.к. ваш оператор приведения к char* работает токо с константными объектами, а неявные приведения в c++ недопустимы.
short* s = NULL;
char* p = &s; //error C2440: 'initializing' : cannot convert from 'short **__w64 ' to 'char *' (MSVC++.NET2002)
[In theory there is no difference between theory and practice. In
practice there is.]
[Даю очевидные ответы на риторические вопросы]