Pytanko do Wirtualnego konstruktora kopiującego :)

0
 #include <iostream>
 using namespace std;

 class Mammal
 {
 public:
	 
 
 Mammal():itsAge(1) { cout << "Konstruktor klasyMammal...\n"; }
 virtual ~Mammal() { cout << "Destruktor klasy Mammal...\n";
}
 Mammal (const Mammal & rhs);
 virtual void Speak() const { cout << "Ssak mowi!\n"; }
virtual Mammal* Clone() ;
 int GetAge()const { return itsAge; }
 protected:
 int itsAge;
 };
 Mammal* Mammal::Clone()
 {
	 cout<<"W MAMAL CLONE\n";
	  return new Mammal(*this); 
 }
 Mammal::Mammal (const Mammal & rhs):itsAge(rhs.GetAge())
 {
 cout << "Konstruktor kopiujacy klasy Mammal...\n";
 }

class Dog : public Mammal
 {
 public:
 Dog() { cout << "Konstruktor klasy Dog...\n"; }
 virtual ~Dog() { cout << "Destruktor klasy Dog...\n"; }
 Dog (const Dog & rhs);
 void Speak()const ;
 virtual Mammal* Clone();
 };

Mammal* Dog::Clone ()
{
 cout<<"W PSIE CLONE\n";
return  new Dog(*this);
}
void Dog::Speak () const 

{
	cout << "Hau!\n"; 
return ;
}

 Dog::Dog(const Dog & rhs):
 Mammal(rhs)
 {
 cout << "Konstruktor kopiujacy klasy Dog...\n";
}

class Cat : public Mammal
{
 public:
 Cat() { cout << "Konstruktor klasy Cat...\n"; }
 ~Cat() { cout << "Destruktor klasy Cat...\n"; }

 Cat (const Cat &);
 void Speak()const { cout << "Miau!\n"; }
 virtual Mammal* Clone() { return new Cat(*this); }
 };

 Cat::Cat(const Cat & rhs):
 Mammal(rhs)
 {
 cout << "Konstruktor kopiujacy klasy Cat...\n";
 }

 enum ANIMALS { MAMMAL, DOG, CAT};
const int NumAnimalTypes = 3;
 int main()
 {
Mammal *theArray[NumAnimalTypes];
 Mammal* ptr;
 int choice, i;
 for ( i = 0; i<NumAnimalTypes; i++)
 {
 cout << "(1)dog (2)cat (3)Mammal: ";
 cin >> choice;
 switch (choice)
 {
 case DOG: ptr = new Dog;
 break;
 case CAT: ptr = new Cat;
 break;
 default: ptr = new Mammal;
 break;
 }
 theArray[i] = ptr;
 }
 Mammal *OtherArray[NumAnimalTypes];
 for (i=0;i<NumAnimalTypes;i++)
 {
 theArray[i]->Speak();
OtherArray[i]=theArray[i]->Clone();
 }
 //for (i=0;i<NumAnimalTypes;i++)
 //OtherArray[i]->Speak();
 //OtherArray[1]=theArray[1]->Clone();

 return 0;
 
 }

NO i takie pytanie bo nie bardzo wiem jak dochodzi do tego kopiowania

 for (i=0;i<NumAnimalTypes;i++)
 {
 theArray[i]->Speak();
OtherArray[i]=theArray[i]->Clone();
 }

Chodzi mi o funkcje CLONE ktora jest Virtualna , po jej wywolaniu jest zwracany nowy adres , return new Dog(*this); . Tak tak :) a potem se OTHERARRAY odpala Kon.Kopiujący i kopiuje co mu tam wpiszemy . Tak ?
Bo jak to kompiluje to mi jeszcze sie odpala kopiujący z DOGA nie wiem z jakiej paki ?

a konstruktora kopiującego nie ma a chce go zrozumieć i tak siedzę i nie wiem do końca jak ten kod sie porusza po między tymi funkcjami , ehh ;)

0
#include <iostream>
using namespace std;

class Mammal {
public:
    Mammal() : itsAge(1) {
        cout << "Konstruktor klasy Mammal...\n";
    }
    Mammal(const Mammal & rhs) : itsAge(rhs.GetAge()) {
        cout << "Konstruktor kopiujacy klasy Mammal...\n";
    }
    virtual ~Mammal() {
        cout << "Destruktor klasy Mammal...\n";
    }

    virtual void Speak() const {
        cout << "Ssak mowi!\n";
    }
    virtual Mammal* Clone() {
        cout << "mammal clone()\n";
        return new Mammal(*this);
    }

    int GetAge()const {
        return itsAge;
    }
    
protected:
    int itsAge;
};

class Dog : public Mammal {
public:
    Dog() {
        cout << "Konstruktor klasy Dog...\n";
    }
    Dog(const Dog & rhs) : Mammal(rhs) {
        cout << "Konstruktor kopiujacy klasy Dog...\n";
    }
    virtual ~Dog() {
        cout << "Destruktor klasy Dog...\n";
    }
    
    virtual void Speak() const {
        cout << "Hau!\n";
        return;
    }
    virtual Mammal* Clone() {
        cout << "psi clone()\n";
        return new Dog(*this);
    }
};

class Cat : public Mammal {
public:
    Cat() {
        cout << "Konstruktor klasy Cat...\n";
    }
    Cat(const Cat & rhs) : Mammal(rhs) {
        cout << "Konstruktor kopiujacy klasy Cat...\n";
    }
    virtual ~Cat() {
        cout << "Destruktor klasy Cat...\n";
    }

    virtual void Speak()const {
        cout << "Miau!\n";
    }
    virtual Mammal* Clone() {
        cout << "koci clone()\n";
        return new Cat(*this);
    }
};

enum ANIMALS {
    MAMMAL,
    DOG, 
    CAT
};
const int NumAnimalTypes = 3;

int main() {
    Mammal* theArray[NumAnimalTypes];
    Mammal* ptr;
    int choice;
    for (int i = 0; i < NumAnimalTypes; ++i) {
        cout << "(1)dog (2)cat (3)Mammal: ";
        cin >> choice;
        switch (choice) {
            case DOG: ptr = new Dog;
                break;
            case CAT: ptr = new Cat;
                break;
            default: ptr = new Mammal;
                break;
        }
        theArray[i] = ptr;
    }
    
    Mammal* OtherArray[NumAnimalTypes];
    for (int i = 0; i < NumAnimalTypes; ++i) {
        theArray[i]->Speak();
        OtherArray[i] = theArray[i]->Clone();
    }

    for (int i = 0; i < NumAnimalTypes; ++i) {
        delete theArray[i];
        delete OtherArray[i];
    }
    
    return 0;
}

Uruchomienie:

$ ./cpp.exe
(1)dog (2)cat (3)Mammal: 1
Konstruktor klasy Mammal...
Konstruktor klasy Dog...
(1)dog (2)cat (3)Mammal: 2
Konstruktor klasy Mammal...
Konstruktor klasy Cat...
(1)dog (2)cat (3)Mammal: 3
Konstruktor klasy Mammal...
Hau!
psi clone()
Konstruktor kopiujacy klasy Mammal...
Konstruktor kopiujacy klasy Dog...
Miau!
koci clone()
Konstruktor kopiujacy klasy Mammal...
Konstruktor kopiujacy klasy Cat...
Ssak mowi!
mammal clone()
Konstruktor kopiujacy klasy Mammal...
Destruktor klasy Dog...
Destruktor klasy Mammal...
Destruktor klasy Dog...
Destruktor klasy Mammal...
Destruktor klasy Cat...
Destruktor klasy Mammal...
Destruktor klasy Cat...
Destruktor klasy Mammal...
Destruktor klasy Mammal...
Destruktor klasy Mammal...

No, to czego nie rozumiesz?

0

koci clone()
Konstruktor kopiujacy klasy Mammal...
Konstruktor kopiujacy klasy Dog...

OtherArray[i] = theArray[i]->Clone(); dzialanie przechodzi tutaj

Mammal* Dog::Clone ()
{
cout<<"W PSIE CLONE\n";
return new Dog(*this);
}

JEst zwracana wartosc znwou wraca tutaj

OtherArray[i] = theArray[i]->Clone()

tak , bo nie jestem pewien.
Potem konstruktor kopiujacy

Mammal::Mammal (const Mammal & rhs):itsAge(rhs.GetAge())
{
cout << "Konstruktor kopiujacy klasy Mammal...\n";
}

no i powrot do kodu glownego , no ja tak to rozumiem , ale wiem ze jest zle .
Jak to powinno być prawidłowo ?

0

Mogę Cię prosić, żebyś spróbował jakoś... Jaśniej wyrazić swoje wątpliwości? Bo z tego co napisałeś, to ciężko mi odczytać Twoje intencje.

Konstruktor kopiujący masz wywołany tutaj:

Dog(*this)
0

Chodzi mi zeby mi pokazac jak porusza sie program od tej linijki gdzie skacza i co robi :) bo nie moge tego zrozumiec .
OtherArray[i] = theArray[i]->Clone();

Wiesz nie wiem za bardzo jak to leci , no bo gdzie po tej linijcie przechodzi kod ? co jest wykonywane najpierw zwrocenie wartosci, wlaczenie konstrukotora kpiujacego , czy moze cos innego. I tak az program wroci do bloku glownego . MOzesz mi napisac co sie dzieje ? bo ja sie zgubiłem :(

0

Odpal debugger i leć krok po kroku patrząc na wartości poszczególnych zmiennych, stos i się zorientujesz.

1 użytkowników online, w tym zalogowanych: 0, gości: 1