There is a class like below:
class vetor
{
protected:
int x, y;
public:
//Construtor
vetor(int _x=0, int _y=0);
};
and another:
class pos : public vetor
{
public:
//Construtor
pos();
umaFuncaoQualquer(int _x=0, int _y=0);
outraFuncaoQualquer(int _x=0, int _y=0, bool algo=false);
maisUmaFuncaoQualquer(void);
};
I want to do this:
vetor v(1, 3);
pos p;
p = v;
How would you do this in C ++? Need to create a function for this, or am I thinking the wrong way?
Up 1:
I was able to do this in the vector class:
class vetor
{
protected:
int x, y;
public:
//Construtor
vetor(int _x=0, int _y=0);
//Retorna x
int getX(void);
//Retorna y
int getY(void);
};
in class pos:
class pos : public vetor
{
public:
//Construtor default
pos();
/*
Construtor para receber o vetor. É responsável por atribuir a x e y
herdados pela classe vetor o x e y de uma instância vetor atribuída,
utilizando as funções getX e getY da classe vetor,
que pega o x,y que estão protected na mesma.
Sua implementação:
pos::pos(vetor _v)
{
x = _v.getX();
y = _v.getY();
};
*/
pos(vetor _v);
umaFuncaoQualquer(int _x=0, int _y=0);
outraFuncaoQualquer(int _x=0, int _y=0, bool algo=false);
maisUmaFuncaoQualquer(void);
};
so the code below works:
vetor v(1, 3);
pos p;
p = v;
However, I would like to know other ways to do the same, maybe there are ways that will fit better in this situation. And I did not understand very well because this way works and the first does not, if both have x and y. I remember reading about it sometime:
//Funciona sem precisar do construtor de cópia
v = p;
//Apenas funciona se tiver um construtor de cópia
p = v;
I read that it is so, but I can not remember why?
Up 2:
Regarding the answer from @Ossetian_Odin, I still do not understand some points, I'll start from the top p bass:
1 - Regarding the use of friend, it would not be necessary in the case of my example, since the members of the "vector" class are protected and not private, so they can already be accessed by the derived class. And the copy constructor that the compiler declares is in the class itself receiving the class itself, how do I assign a different one, requires that I create a constructor for this, the problem is that I created in the class "pos" and not in the "vector" , which is somewhat confusing, but could not be the opposite, since following the logic "is a", "vector" is not a "pos", but "pos" is a "vector". Am I right?
2 - As for downcasting, you say to create an abstract class, so would I create the "vector" class as abstract? Would not it be better to use dynamic_cast in this case? And if I use "dynamic_cast" what happens when I do "dynamic_cast (& v);", p will have all the variables and functions of "pos" (since it inherits "vector", are the of "vector" and those that only exist in "pos"), receiving the assignment of v the variables that have in common, or p have only what "vector" has?
Up 3:
I gave it one study, and I realized that the way I did in Up 2 is the right one for this case, at least the most certain I've found so far. For what I want to do is assign a "vector" object to an "pos" object in the most "abstract" possible way, abstraction for another person to use, it is easier to do "p = v;" than "static_cast (& vet);" for example; what I need is not a downcasting, nor to use polymorphism, but I am grateful for both answers that made me study these concepts more, because what is really needed in this case is simply to assign "p = v", where p a constructor to handle the assigned class will work, not conversion (polymorphism) it needs, but a copy. I gave a read in a material that I found in google, and in the chapter "Conversion of pointers" clarified me:
Translating to the question variables:
vetor v, *pv; // pv pode apontar para objetos do tipo vetor e derivados
pos p, *pp; // pp pode apontar para objetos do tipo pos e derivados
v = p; // copia v parte vetor de p para v (não é conversão)
p = v; /* erro! v pode não ter todos elementos para a cópia(A questão se
refere a este caso, que também não é conversão, e pode ser
resolvido da maneira do Up 2, o que queria saber era se existe
outras maneiras alem da que fiz no Up 2, que seria para cópia,
e não mudança de forma ou tratar um classe como uma
diferente)
*/
pv = &v; // ok
pv = &p; // ok, pv aponta para um objeto do tipo pos
pp = pv; // erro! pv pode apontar para um objeto do tipo vetor
pp = &p; // ok
pp = &v; // erro! pp não pode apontar para objetos do tipo vetor