I
iakko
Hello everybody,
first of all, sorry if the question I'm about to ask have been already
answered, but after some hours spent bothering google, I did not work
it out.
I have two classes:
class Base
{
public:
Base() { k = 0; };
~Base() {};
int k;
void setK(int p) { k = p; };
int getK() { return k; };
void myMethod() { /* Code here */ };
};
class Derived : public Base
{
public:
Derived() : Base() { i = 0; };
~Derived() {};
int i;
void setI(int p) { i = p; };
int getI() { return i; };
void myCoolMethod() { /* Code here */ };
};
Suppose I need to work with the derived class because I have to
implement many methods and I cannot touch the Base class since it's a
Qt class.
What I would like to do is find a way to "merge" an implementation of
a Derived class with another implementation of Base class, due to have
all the data for the Base class avaiable in the Derived class.
Casting is not sufficient since if I cast, I can use the Derived class
but I will lose all the personal data referred to it.
Let me explain with an tiny working example:
/* ### EXAMPLE STARTS ### */
#include <iostream>
class Base
{
public:
Base() { k = 0; };
~Base() {};
int k;
void setK(int p) { k = p; };
int getK() { return k; };
void myMethod() { /* Code here */ };
};
class Derived : public Base
{
public:
Derived() : Base() { i = 0; };
~Derived() {};
int i;
void setI(int p) { i = p; };
int getI() { return i; };
void myCoolMethod() { /* Code here */ };
};
int main() {
Derived * d = new Derived();
d->setI(10);
printf("i: %d\n", d->getI());
/* prints 10 */
Base * b = new Base();
b->setK(2);
printf("k: %d\n", b->getK());
/* prints 2 */
d = reinterpret_cast<Derived *>(b);
printf("i: %d -- k: %d\n", d->getI(), d->getK());
/* prints 0 -- 2 !!! I want 10 -- 2 !!!*/
return 0;
}
/* ### EXAMPLE STOPS ### */
Casting from Base to Derived loses all the Derived class information!!
I feel like there is a solution but I cannot imagine what is it.
Any suggestion is appreciated, thanks.
first of all, sorry if the question I'm about to ask have been already
answered, but after some hours spent bothering google, I did not work
it out.
I have two classes:
class Base
{
public:
Base() { k = 0; };
~Base() {};
int k;
void setK(int p) { k = p; };
int getK() { return k; };
void myMethod() { /* Code here */ };
};
class Derived : public Base
{
public:
Derived() : Base() { i = 0; };
~Derived() {};
int i;
void setI(int p) { i = p; };
int getI() { return i; };
void myCoolMethod() { /* Code here */ };
};
Suppose I need to work with the derived class because I have to
implement many methods and I cannot touch the Base class since it's a
Qt class.
What I would like to do is find a way to "merge" an implementation of
a Derived class with another implementation of Base class, due to have
all the data for the Base class avaiable in the Derived class.
Casting is not sufficient since if I cast, I can use the Derived class
but I will lose all the personal data referred to it.
Let me explain with an tiny working example:
/* ### EXAMPLE STARTS ### */
#include <iostream>
class Base
{
public:
Base() { k = 0; };
~Base() {};
int k;
void setK(int p) { k = p; };
int getK() { return k; };
void myMethod() { /* Code here */ };
};
class Derived : public Base
{
public:
Derived() : Base() { i = 0; };
~Derived() {};
int i;
void setI(int p) { i = p; };
int getI() { return i; };
void myCoolMethod() { /* Code here */ };
};
int main() {
Derived * d = new Derived();
d->setI(10);
printf("i: %d\n", d->getI());
/* prints 10 */
Base * b = new Base();
b->setK(2);
printf("k: %d\n", b->getK());
/* prints 2 */
d = reinterpret_cast<Derived *>(b);
printf("i: %d -- k: %d\n", d->getI(), d->getK());
/* prints 0 -- 2 !!! I want 10 -- 2 !!!*/
return 0;
}
/* ### EXAMPLE STOPS ### */
Casting from Base to Derived loses all the Derived class information!!
I feel like there is a solution but I cannot imagine what is it.
Any suggestion is appreciated, thanks.