View Single Post
Old 09-09-2004, 06:35 PM   #2 (permalink)
bacon
Upright
 
I don't mean this to be harsh, but you need to know where you stand. The fact that you think the first example should work shows you lack a fundamental understand of what you are actually doing.

A struct is a new type. Its just a representation of how a chunk of memory of a particular size should be parsed. Even if two structs are identical, you can't expect the compiler to notice and allow implicit casting.

Its the same as:

Code:
int foo;
float bar;
foo = bar
Its a type mismatch.

Now, if the structs are identical, and you want to futz around with pointers a bit, you might have some luck.

Code:
Foo foo;
Bar *bar = (Bar*)&foo;
That would probably build, and might work. But is ickybad voodoo. Stuff you get fired for doing in the real world.

As for the "right" way to do it... Well, remember, in c++ a struct is a just a class with all public members. So, you could either create a copy constructor, or overload the equals operator. Consult your c++ book for details, or google it up.

Chances are, you don't actually want to do what you are trying. There is probably a more direct way to solve whatever problem you are working at.

As far as the template stuff working.... Templates are one of the features of C++ that makes Java a better OO language.
bacon is offline  
 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73