Pages: 1 [2]   Go Down
Author Topic: Scope problem  (Read 808 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 160
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks a lot for the info. Gave me a lot of good ideas, mostly about the efficiency of unions.


RobertEagle
Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 52
Posts: 1776
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The union method is becoming very popular, avoids nasty casts with pointers and provides a nice OO syntax.

Here is a type safe method.

Code:
template< typename _Type > union Serializer{
  _Type  value;
  byte    data[ sizeof( _Type ) ];
};

//You would use like
class itg3200
{
private:
Serializer< int > output[3];
protected:
...
Logged


UK
Offline Offline
Shannon Member
****
Karma: 183
Posts: 11138
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you wanted to be really clever, you could do this:

If you don't actually need to access the data as an integer, you could use a struct rather than a union.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24298
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
And it proves that people are just subjective
Speak for yourself!   smiley-yell
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: 1 [2]   Go Up
Jump to: