Mail Archives: djgpp/2002/02/13/01:33:15
Well technically speaking since C is roughly a subset of C++ your code is
already "transformed".
First I would ask why you don't want to use templates? Templates are
perfectly suited for what you want to accomplish.
Without templates I'm afraid you are "stuck" with the casts. In C and C++ a
pointer to anything is a void* which you obviously know. As for the rest of
your code, are you trying to win an obsfucated C++ contest? :)
I'm kidding, but here are a couple of comments about the code you posted.
1. typedefs for structs are not needed in C++.
2. "this" is a keyword in C++, so you can't use it as a variable name.
3. Maybe this is your question and I am just stating the obvious but you
have to change your 3 functions from this
> int* get2(A* this) {return this->_p;}
> double* get3(A* this) {return this->_p;}
> char* get4(A* this) {return this->_p;}
to this.
int* get2(A* that) {return (int*)that->_p;}
double* get3(A* that) {return (double*)that->_p;}
char* get4(A* that) {return (char*)that->_p;}
In C++ you can't assign a void* to a non void* without doing an explicit
cast.
4. The agregate assignment of your four variables A,B,C,D are probably
handled better by constructors.
5. I am probably missing the overall gist of what you are trying to do. But
if you want the _p member of struct A to be accesible as an int, double and
char .. wouldn't you be better of just using a union?
6. Finaly here is roughly the equivalent C++ code with templates. Note I
used the c io routines rather than the C++ iostreams so as not to further
complicate the issue, made everything public for the same reason, and also
left out other C++ idioms (like using const, ctor's, etc.).
template<typename T> class holder
{
public:
T value_;
};
int main(int argc,char* argv[])
{
int value = 66;
holder<int> b = {value};
holder<double> c = {value};
holder<char> d = {value};
printf("I am of type \"int\". My value is %i\n",(b.value_));
printf("I am of type \"double\". My value is %f\n",(c.value_));
printf("I am of type \"char\". My value is %c\n",(d.value_));
return 0;
}
Hope that helps,
Bob
"Traveler" <traveler AT netti DOT fi> wrote in message
news:a48ggn$f8n$1 AT tron DOT sci DOT fi...
> 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);
> }
[ 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! ]
- Raw text -