| www.delorie.com/archives/browse.cgi | search |
| X-Authentication-Warning: | delorie.com: mailnull set sender to djgpp-bounces using -f |
| From: | "Traveler" <traveler AT netti DOT fi> |
| Newsgroups: | comp.lang.c++,comp.lang.c++.moderated,comp.os.msdos.djgpp |
| Subject: | Heterogenous object container without templates & type casting ??? |
| Date: | 12 Feb 2002 03:50:10 -0500 |
| Organization: | SAUNALAHDEN asiakas |
| Lines: | 65 |
| Sender: | cppmods AT netlab DOT cs DOT rpi DOT edu |
| Approved: | hsutter AT acm DOT org |
| Message-ID: | <a47716$eiq$1@tron.sci.fi> |
| NNTP-Posting-Host: | netlab.cs.rpi.edu |
| X-Original-Date: | Mon, 11 Feb 2002 03:42:34 +0200 |
| X-Submission-Address: | c++-submit AT netlab DOT cs DOT rpi DOT edu |
| X-Auth: | PGPMoose V1.1 PGP comp.lang.c++.moderated |
| iQBVAwUAPGjXPkHMCo9UcraBAQFUBAH/cf3jS/bQDVDpNdYPycAGzCinqeDvwAsJ | |
| 4TftSlGcHhebQcUbVz5BQde3gerJsbWwDBHwsQZUlucxB7MvkPzSzQ== | |
| =ayQE | |
| To: | djgpp AT delorie DOT com |
| DJ-Gateway: | from newsgroup comp.os.msdos.djgpp |
| Reply-To: | djgpp AT delorie DOT com |
Hi
Is it possible to transform the following C code to C++ so that there is no
need for type-casting ?
I am trying to make a general object class that can contain any type without
the use of templates.
#include <stdio.h>
#include <stdlib.h>
typedef struct A
{
void* _p;
void* (*get)(struct A* this);
} A;
typedef struct
{
int* (*get)(A* this);
} B;
typedef struct
{
double* (*get)(A* this);
} C;
typedef struct
{
char* (*get)(A* this);
} D;
void* get1(A* this) {return this->_p;}
int* get2(A* this) {return this->_p;}
double* get3(A* this) {return this->_p;}
char* get4(A* this) {return this->_p;}
int main(int argc,char* argv[])
{
int value = 66;
A a = {&value,get1}; // Simple & dangerous way to "put"
something in to void pointer "a._p"
// because I had no
patience to play games with "malloc" in this case.......
B b = {get2}; // Takes care of the type casting....
C c = {get3};
D d = {get4};
printf("I am of type \"int\". My value is %i\n",*b.get(&a));
printf("I am of type \"double\". My value is %d\n",*c.get(&a));
printf("I am of type \"char\". My value is %c\n",*d.get(&a));
return(0);
}
Traveler
traveler AT netti DOT fi
[ Send an empty e-mail to c++-help AT netlab DOT cs DOT rpi DOT edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
| webmaster | delorie software privacy |
| Copyright © 2019 by DJ Delorie | Updated Jul 2019 |