このコード:
class foo
{
int x;
public:
foo(int x) : x(x) { }
int get() const { return x; }
//...
};
class bar
{
int x;
public:
bar(const foo& x) : x(x.get()) { }
int get() const { return x; }
bar& operator =(const foo& rhs) { x = rhs.get(); return *this; }
//...
};
void func()
{
foo f = 3;
bar b = 3;
b = 7;
//...
}
行のエラーbar b = 3
(g ++ 4.7.1 with -fstd=gnu++11
):
error: conversion from 'int' to non-scalar type 'bar' requested
ただし、を受け取るbar
コンストラクターを提供していfoo
ます。intは、その前の行に示されているように暗黙的に変換できますfoo
。では、何が問題になっていますか?
ちなみに、いくつかの理由から、変換を強制的にfoo
使用することは望ましくfoo(3)
ありません。これは、実際のコードを使用して読み取るのが醜くなるためです。