09-09-2004, 04:19 PM | #1 (permalink) |
Location: Waterloo, Ontario
|
[C++] struct typing issues
Why is it that this code (correctly, in my opinion) fails to compile:
Code:
struct Foo { int value; }; struct Bar { int value; }; Foo foo; Bar bar = foo; Code:
template<typename T> struct Value { T value; }; typedef Value<int> Foo; typedef Value<int> Bar; Foo foo; Bar bar = foo; Thanks! |
09-09-2004, 06:35 PM | #2 (permalink) |
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 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; 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. |
09-09-2004, 11:43 PM | #3 (permalink) |
Crazy
|
I think that KnifeMissile meant that the first piece of code does not compile, and that that is the correct thing. That code fails to compile because a Foo is not a Bar. A compiler can't "look inside" each struct and decide that they are equal even if their names differ. That's an impossible task. Rather, it looks at their types and makes a decision based on that.
In the second example, the type of Foo and Bar is the same: Code:
typedef Value<int> Foo; typedef Value<int> Bar; Foo foo; Bar bar = foo; Casting struct Foo's to struct Bar's won't necessarily get you fired in the real world as long as you solve a problem that needed to be solved (sometimes you just got to get down and dirty). But it's not a practice I'd advise doing if there's a viable alternative.
__________________
I want no escape. |
09-09-2004, 11:45 PM | #4 (permalink) | ||
Location: Waterloo, Ontario
|
Perhaps I do mean to be harsh when I say this but you should really learn to read. Can you see why:
Quote:
Quote:
Now, for clarification (at least one of you seems to need it), I expect different struct declarations to be considered different types (hence, my statement about "(correctly, IMO) fails to compile"). My question was why templated structs aren't considered different types... It's tempting to debate the merits of C++ versus Java but that would honestly diverge from the thread topic and risk starting a religious flame war... |
||
09-10-2004, 12:44 AM | #5 (permalink) | |
Junkie
Location: San Francisco
|
Has been answered, but I typed it and posted it and its staying. =P
Quote:
Code:
template<class T> struct s1 { T x; }; template<class T> struct s2 { T x; }; typedef s1<int> Foo; typedef s2<int> Bar; Then Code:
Foo f; Bar b = f; Last edited by n0nsensical; 09-10-2004 at 12:47 AM.. |
|
Tags |
issues, struct, typing |
|
|