次の許容可能なプログラミング手法は次のとおりです。
class TestA
{
protected:
int A;
public:
TestA(){A = 10;}
TestA &operator=(const TestA &ItemCopy)
{
A = ItemCopy.A;
printf("A is: %d!\n",A);
return *this;
}
};
class TestB : public TestA
{
protected:
int B;
public:
TestB(){A = 20; B = 30;}
operator const TestA&(){ return *this; } //Note returns reference, upcasts implicitly.
};
int main()
{
TestA Test;
TestB Test2;
Test = Test2; //Calls Test2's (AKA TestB's) conversion operator
return 0;
}
許容できる/許容できない理由は何ですか?
(TestAでTestB代入演算子を作成することについて、明白な提案をしないでください。これは、アップキャストおよび/または変換演算子をこの方法で使用する必要があるかどうかに関する質問です)。
また、質問の賛成/反対のコメントにフィードバックを残して、将来質問を改善できるようにすることをお勧めします。