Knowing which child class the parent class points to

3

I came from java and am having a question about the C ++ polymorphism.

Given these classes:

class Empresa{
vector<Pessoa> pessoas;
int empregados;
addPessoa(Pessoa* p)
}

class Pessoa {
...
};


class Empregado:public Pessoa{
...
}

class Dono:public Pessoa{
...
}

e a função:

addPessoa(Pessoa* p){
pessoas.push_back(*p);
if(...) // pessoa é um empregado
empregados++;
}

I'm not able to implement this if .

    
asked by anonymous 22.10.2017 / 19:00

1 answer

4

The solution to this is the same as Java, if the function should add a Empregado then do the receive function rather than the general class. As for the other positions.

addEmpregado(Empregado* p) {
    pessoas.push_back(*p);
    funcionarios++;
}

Who will call the function knows what type of object it is, then it is the one that should decide what to call.

I will not go into the merits that this heritage may not be appropriate. There are lots of inheritance abuse .

    
22.10.2017 / 19:12