473,396 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Help! dynamic classes (shapeshifter)

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
Jul 19 '05 #1
2 2038

"Timothy Stark" <sw**********@speakeasy.org> wrote in message news:Ed********************@speakeasy.net...
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).
Jul 19 '05 #2

"Ron Natalie" <ro*@sensor.com> wrote in message
news:3f*********************@news.newshosting.com. ..
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
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Kieran | last post by:
Hi I have 2 classes, layout1 and layout2, both return different table layouts In my controlling program I create one of these tables using Table myTable = new layout1 I need to...
6
by: moi | last post by:
hi, im fairly new to c++ and as part of a module at uni im learning it. i know the basics i suppose, but as our final hand-in we have to alter code we wrote for an earlier assignment to use...
9
by: Gb | last post by:
Hi, just a quick (maybe stupid) question .. Can be possible to link a table dynamically using this logic?: --@@@ SQL START SELECT Value as myvar, m.field1,m.field2,m.field3 FROM -- I want to...
35
by: A Jafarpour | last post by:
Hi everyone, hope someone can tell if there is any way to, dynamically, build an statement and then call some functions to execute the statement. I know examples always help, so here what I am...
5
by: RBCC | last post by:
Does anyone know how to dynamically allocate array of structures? Or where Ican find info on this on the web? John
3
by: Dave Rose | last post by:
Hello all. I was wondering if creating classes could be dynamic. I want to know if I can make a class Person, then read in a list of names (say people's names) so then I can have a class instance...
4
by: Mr. Smith | last post by:
Hi. What's the correct syntax here: (given the recordset myRS with x records do until myRS.EOF txtvar & myRS("serial_number") = myRS("serial_name") myRS.movenext loop ..asp will not build...
1
by: foker | last post by:
I have a file with a list of times in it in the form hh:mm:ss. The file starts with a single INT like 460. That int is the number of times in the file so I can size my dynamic array correctly. I...
2
by: Christina | last post by:
Hello !! I am creating a dynamic textbox and want to validate it using the requiredfieldvalidator. These are the steps which I tried: ==================================================== 1)...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.