User forums > General (but related to Code::Blocks)
arrays - what the heck?
Balazs:
Consider this code:
--- Code: ---struct Test {
int a[4][4];
};
int main()
{
Test t1, t2;
t1 = t2;
return 0;
}
--- End code ---
It compiles fine. I ask: what the heck? It happily assigns the array, and not as pointer but it actually COPIES the whole thing correctly (I checked it)! But if I do this:
--- Code: ---int main()
{
int a1[4][4], a2[4][4];
a1 = a2;
return 0;
}
--- End code ---
I get this error:
error: ISO C++ forbids assignment of arrays
So, can someone clarify me here what the hell is happening? If ISO C++ forbids array assignment, then why does it allow it, if it is inside a struct (or class)? Is it only GCC's behaviour or is it valid C++?
Thanks, and sorry for being a bit off-forum.
--
Greets,
B.
EDIT: I just read, that this is not a general programming board. Well, I already posted, so I apologize, but I promise to keep myself from posting things like this in the future.... IF i get the answer :)
MortenMacFly:
--- Quote from: Balazs on August 14, 2006, 12:57:49 pm ---error: ISO C++ forbids assignment of arrays
--- End quote ---
In the first case you assign a struct in the second you assign an array. It isn't suprising at all.
--- Quote from: Balazs on August 14, 2006, 12:57:49 pm ---Thanks, and sorry for being a bit off-forum.
--- End quote ---
Allthough you are sorry for this you could have also asked in a C/C++ related forum. Why didn't you?
With regards, Morten.
Balazs:
--- Quote from: MortenMacFly on August 14, 2006, 01:08:24 pm ---In the first case you assign a struct in the second you assign an array. It isn't suprising at all.
--- End quote ---
Yeah, but if you look closely, you'll see, that there is an array INSIDE that struct, and it gets assigned, as if you did a memcpy. Why is that? Is it supposed to happen like that?
MortenMacFly:
--- Quote from: Balazs on August 14, 2006, 01:11:55 pm ---Yeah, but if you look closely, you'll see, that there is an array INSIDE that struct, and it gets assigned, as if you did a memcpy. Why is that? Is it supposed to happen like that?
--- End quote ---
I have looked closely and I still insist of what I said.
Another hint from me: Think of the struct as class.
And another hint: If you want more detailed answers ask in a C/C++ only related forum. ;-)
Balazs:
--- Quote from: MortenMacFly on August 14, 2006, 01:22:55 pm ---Another hint from me: Think of the struct as class.
And another hint: If you want more detailed answers ask in a C/C++ only related forum. ;-)
--- End quote ---
Cmon, what's good in holding back the answer??? :roll:
I know that the struct is a class (or vice versa, how we look at it), but that still doesn't explain why the default assignment operator generated by the compiler for that class allows assigning an array, when it is generally not allowed.
Navigation
[0] Message Index
[#] Next page
Go to full version