Help! dynamic classes (shapeshifter)

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
 
R

Ron Natalie

Timothy Stark said:
a = new test1;
b = (test2 *)a;

This is bogus. You can't convert test1 to test2 sanely. The C-style cast
in this case ends up being a reinterpret_cast.

Just what are you trying to accomplish? The normal paradigm is to
convert test1 and test2 instances to their common base (socket).
 
T

Timothy Stark

Ron Natalie said:
This is bogus. You can't convert test1 to test2 sanely. The C-style cast
in this case ends up being a reinterpret_cast.

Oh, well. Thank you. I tried that and it did not work. I have to look for
different method
to use dynamic function calls.
Just what are you trying to accomplish? The normal paradigm is to
convert test1 and test2 instances to their common base (socket).

In my program, I want to use simple dynamic function call (callback
function) to the specific process routine from handle routine (incoming data
from TCP/IP network). I would be consider 'this->*' method. For example,
there are two different classes that inherit to that same socket class.
Will 'this->*' work on different class that inherit the socket class? How
about dynamic virtual function calls, etc? I am looking for some methods
because I am new to C++ programming.

class Socket {
public:
int idSocket;
void (*process)(char *, int);

static void Handle(void);

inline void SetCallback(void (*newcall)(char *, int))
{ process = newcall; }
};

class test1 : public Socket
{
:
void process2(char *, int);
void process1(char *, int);
};

class test2 : public Socket
{
:
void process3(char *, int);
void process4(char *, int);
};

void Socket::Handle(void)
{
Socket *sck;

:
:
len = read(idSocket, data, sizeof(data));
(sck->*process)(data, len);
}

Thank you!
Tim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top