T
Timothy Stark
Hello folks,
I am working on my program and was trying to implement 'dynamic' classes
by using virtual function calls but it did not work. I want
to convert from 'class test1' to 'class test2' without affecting
stored variables much like class shapeshifter. That's why I need
that for my own socket interface. Also I want to print "Test #2 (1)"
from 'b'. Instead it prints "Test #1 (1)".
For example,
a = new test1;
b = (test2 *)a;
c = new test2;
It results: (this program prints that
Test #1 (1)
Test #1 (1)
Test #2 (2)
Here is my test program.
#include <stdio.h>
class socket {
public:
int idSocket;
virtual void print(void) {}
};
class test1 : public socket
{
public:
void print(void)
{
printf("Test #1 (%d)\n", idSocket);
}
};
class test2 : public socket
{
public:
void print(void)
{
printf("Test #2 (%d)\n", idSocket);
}
};
int main(void)
{
test1 *a;
test2 *b;
test2 *c;
a = new test1;
a->idSocket = 1;
b = (test2 *)a;
c = new test2;
c->idSocket = 2;
a->print();
b->print();
c->print();
return 0;
}
Thank you!
Tim
I am working on my program and was trying to implement 'dynamic' classes
by using virtual function calls but it did not work. I want
to convert from 'class test1' to 'class test2' without affecting
stored variables much like class shapeshifter. That's why I need
that for my own socket interface. Also I want to print "Test #2 (1)"
from 'b'. Instead it prints "Test #1 (1)".
For example,
a = new test1;
b = (test2 *)a;
c = new test2;
It results: (this program prints that
Test #1 (1)
Test #1 (1)
Test #2 (2)
Here is my test program.
#include <stdio.h>
class socket {
public:
int idSocket;
virtual void print(void) {}
};
class test1 : public socket
{
public:
void print(void)
{
printf("Test #1 (%d)\n", idSocket);
}
};
class test2 : public socket
{
public:
void print(void)
{
printf("Test #2 (%d)\n", idSocket);
}
};
int main(void)
{
test1 *a;
test2 *b;
test2 *c;
a = new test1;
a->idSocket = 1;
b = (test2 *)a;
c = new test2;
c->idSocket = 2;
a->print();
b->print();
c->print();
return 0;
}
Thank you!
Tim