d04 ex02 dynamic_cast dans main pour ne pas avoir une metode pure brain dans animal au cas ou en correction...

This commit is contained in:
Hugo LAMY
2022-03-16 12:42:13 +01:00
parent 1bae971027
commit bac6161af8
8 changed files with 16 additions and 12 deletions

View File

@@ -13,7 +13,7 @@ ifeq "$(TYPE)" "c"
CC = c CC = c
EXT = c EXT = c
else ifeq "$(TYPE)" "cpp" else ifeq "$(TYPE)" "cpp"
CC = clang++ CC = c++
EXT = cpp EXT = cpp
endif endif

View File

@@ -17,7 +17,7 @@ public:
virtual void makeSound() const = 0; virtual void makeSound() const = 0;
std::string getType() const; std::string getType() const;
virtual Brain * getBrain() const = 0; // virtual Brain * getBrain() const = 0;
protected: protected:
std::string type; std::string type;

View File

@@ -19,7 +19,7 @@ public:
Cat( Cat const & src ); Cat( Cat const & src );
~Cat(); ~Cat();
Cat & operator=( Cat const & rhs ); Cat & operator=( Cat const & rhs );
Cat & operator=( Animal const & rhs ); // Cat & operator=( Animal const & rhs );
void makeSound() const; void makeSound() const;

View File

@@ -17,7 +17,7 @@ public:
Dog( Dog const & src ); Dog( Dog const & src );
~Dog(); ~Dog();
Dog & operator=( Dog const & rhs ); Dog & operator=( Dog const & rhs );
Dog & operator=( Animal const & rhs ); // Dog & operator=( Animal const & rhs );
void makeSound() const; void makeSound() const;
Brain * getBrain() const; Brain * getBrain() const;

View File

@@ -18,25 +18,25 @@ int main() {
std::cout << std::endl; std::cout << std::endl;
std::cout << B_BLUE "cat i : " RESET; std::cout << B_BLUE "cat i : " RESET;
i->getBrain()->printIdea(0); dynamic_cast<Cat*>(i)->getBrain()->printIdea(0);
std::cout << B_BLUE "cat j : " RESET; std::cout << B_BLUE "cat j : " RESET;
j->getBrain()->printIdea(0); dynamic_cast<Cat*>(j)->getBrain()->printIdea(0);
std::cout << "\n" B_BLUE "*i = *j" RESET "\n"; std::cout << "\n" B_BLUE "*i = *j" RESET "\n";
*i = *j; *(dynamic_cast<Cat*>(i)) = *(dynamic_cast<Cat*>(j));
std::cout << B_BLUE "cat i : " RESET; std::cout << B_BLUE "cat i : " RESET;
i->getBrain()->printIdea(0); dynamic_cast<Cat*>(i)->getBrain()->printIdea(0);
std::cout << B_BLUE "cat j : " RESET; std::cout << B_BLUE "cat j : " RESET;
j->getBrain()->printIdea(0); dynamic_cast<Cat*>(j)->getBrain()->printIdea(0);
std::cout << "\n" B_BLUE "j->getBrain->putIdea(\"I am not a cat\")" RESET "\n"; std::cout << "\n" B_BLUE "j->getBrain->putIdea(\"I am not a cat\")" RESET "\n";
j->getBrain()->putIdea(0, "I am not a cat");; dynamic_cast<Cat*>(j)->getBrain()->putIdea(0, "I am not a cat");;
std::cout << B_BLUE "cat i : " RESET; std::cout << B_BLUE "cat i : " RESET;
i->getBrain()->printIdea(0); dynamic_cast<Cat*>(i)->getBrain()->printIdea(0);
std::cout << B_BLUE "cat j : " RESET; std::cout << B_BLUE "cat j : " RESET;
j->getBrain()->printIdea(0); dynamic_cast<Cat*>(j)->getBrain()->printIdea(0);
std::cout << std::endl; std::cout << std::endl;
std::cout << B_BLUE "delete i" RESET "\n"; std::cout << B_BLUE "delete i" RESET "\n";

BIN
d04/ex02/pure Executable file

Binary file not shown.

View File

@@ -67,6 +67,7 @@ Cat & Cat::operator=( Cat const & rhs ) {
} }
// need of a second overload in case "Animal cat" = "Animal cat"; // need of a second overload in case "Animal cat" = "Animal cat";
// https://stackoverflow.com/questions/68248198/why-my-virtual-assignment-operator-not-doing-as-intended // https://stackoverflow.com/questions/68248198/why-my-virtual-assignment-operator-not-doing-as-intended
/*
Cat & Cat::operator=( Animal const & rhs ) { Cat & Cat::operator=( Animal const & rhs ) {
Animal::operator=(rhs); Animal::operator=(rhs);
std::cout << COPLIEN_COLOR "Cat (Animal) assignator" RESET "\n"; std::cout << COPLIEN_COLOR "Cat (Animal) assignator" RESET "\n";
@@ -74,6 +75,7 @@ Cat & Cat::operator=( Animal const & rhs ) {
*_brain = *(rhs.getBrain()); *_brain = *(rhs.getBrain());
return *this; return *this;
} }
*/
/********************************************* /*********************************************
* PUBLIC MEMBER FUNCTIONS * PUBLIC MEMBER FUNCTIONS

View File

@@ -56,6 +56,7 @@ Dog & Dog::operator=( Dog const & rhs ) {
} }
// need of a second overload in case "Animal cat" = "Animal cat"; // need of a second overload in case "Animal cat" = "Animal cat";
// https://stackoverflow.com/questions/68248198/why-my-virtual-assignment-operator-not-doing-as-intended // https://stackoverflow.com/questions/68248198/why-my-virtual-assignment-operator-not-doing-as-intended
/*
Dog & Dog::operator=( Animal const & rhs ) { Dog & Dog::operator=( Animal const & rhs ) {
Animal::operator=(rhs); Animal::operator=(rhs);
std::cout << COPLIEN_COLOR "Cat (Animal) assignator" RESET "\n"; std::cout << COPLIEN_COLOR "Cat (Animal) assignator" RESET "\n";
@@ -63,6 +64,7 @@ Dog & Dog::operator=( Animal const & rhs ) {
*_brain = *(rhs.getBrain()); *_brain = *(rhs.getBrain());
return *this; return *this;
} }
*/
/********************************************* /*********************************************
* PUBLIC MEMBER FUNCTIONS * PUBLIC MEMBER FUNCTIONS