Bonjour à tous.
Probleme de polymorphisme, je vais pété un plomb
Question : comment obtenir = Fils à l'étape 4 5 7 8 sans caster.
Execution
------------ Etape 01 ------------
Creation Pere
------------ Etape 02 ------------
Creation Pere
Creation Fils
------------ Etape 03 ------------
------------ Etape 04 ------------
= Pere
------------ Etape 05 ------------
= Pere
------------ Etape 06 ------------
Creation Pere
Creation Fils
------------ Etape 07 ------------
= Pere
------------ Etape 08 ------------
= Pere
------------ Etape 09 ------------
Do Fils
------------ Etape 10 ------------
Creation Pere
Creation Fils
------------ Etape 11 ------------
Creation Pere
Creation Fils
------------ Etape 12 ------------
= Fils
------------ Etape 98 ------------
Destruction Pere
------------ Etape 99 ------------
Destruction Fils
Destruction Pere
Appuyez sur une touche pour continuer...
Code :
#include <iostream>
using namespace std;
/* *************************************************** */
class Pere
{
public:
Pere::Pere()
{cout << "Creation Pere" << endl;}
virtual Pere::~Pere() {cout << "Destruction Pere" << endl;}
virtual void Pere::Do() {cout << "Do Pere" << endl;}
virtual Pere & operator = (const Pere & source) {cout << "= Pere" << endl; return *this;}
};
/* *************************************************** */
class Fils : public Pere
{
public:
Fils() {cout << "Creation Fils" << endl;}
~Fils() {cout << "Destruction Fils" << endl;}
void Do() {cout << "Do Fils" << endl;}
virtual Fils & operator = (const Fils & source) {cout << "= Fils" << endl; return *this;}
};
/* *************************************************** */
int main(int argc, char *argv[])
{
Pere * Tab[2];
cout << "------------ Etape 01 ------------" << endl;
Tab[0] = new Pere;
cout << "------------ Etape 02 ------------" << endl;
Tab[1] = new Fils;
cout << "------------ Etape 03 ------------" << endl;
Fils * F1;
cout << "------------ Etape 04 ------------" << endl;
* Tab[1] = * F1;
cout << "------------ Etape 05 ------------" << endl;
Tab[1]->operator=(*F1);
cout << "------------ Etape 06 ------------" << endl;
Fils F2;
cout << "------------ Etape 07 ------------" << endl;
* Tab[1] = F2;
cout << "------------ Etape 08 ------------" << endl;
Tab[1]->operator=(F2);
cout << "------------ Etape 09 ------------" << endl;
Tab[1]->Do();
cout << "------------ Etape 10 ------------" << endl;
Fils F3;
cout << "------------ Etape 11 ------------" << endl;
Fils F4;
cout << "------------ Etape 12 ------------" << endl;
F3 = F4;
cout << "------------ Etape 98 ------------" << endl;
delete Tab[0];
cout << "------------ Etape 99 ------------" << endl;
delete Tab[1];
system("PAUSE");
return 1;
}