• Advertisement

Archived

This topic is now archived and is closed to further replies.

When to use friend, and union?

This topic is 5101 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was wondering when do you use the "friend" statement or whatever in C++. Also when is it best to use a union? My friend said unions were faster then a typedef struct because he said they are in the same location in memory, which makes them not use as much memory or something. Im not sure if this is right. Im also not sure when to use it if anyone can give me an idea of when id use it? I also was wondering when you use a friend data type or object type and what type of things it will allow me to do that I can not do normally?

Share this post


Link to post
Share on other sites
Advertisement
1°/ a friend class A of class B is able to access private and protected members of class B.

2°/ an union makes all values within share the same memory space when it''s instatiated. When you want a value to be read either as a long, as two shorts, or four bytes, you turn that value into an union :

union thisOne { long L[1]; short S[2]; char C[4]; }

Where basically L == S == C.

Yes, it does use less memory than a struct, but it can also store less things than a struct (in our case, only 4 bytes worth of data, while a struct would be able to hold 12).

Victor Nicollet, INT13 game programmer

Share this post


Link to post
Share on other sites
I''ll answer the union question, but I''ll pass on the friend for someone else. I don''t use friend.

Here''s an example of union used in a color class:


class GrColor
{
public:
// types and structs
union
{
struct ARGB
{
u8 b;
u8 g;
u8 r;
u8 a;
};

ARGB color;
u32 data;
};

// ctor & dtor
GrColor();
~GrColor();

// public methods
GrColor( u32 colorData );
GrColor( u8 a, u8 r, u8 g, u8 b );
};


Using this class, I can set an GrColor instance like this:
GrColor m_skyColor = GrColor( 0xFF270E67 ); // alpha, red, green, blue
I could also do this:
m_skyColor.r = FF; // results in 0xFFFF0E67

Esentially the union of the ARGB (4 unsigned 8-bit values) struct with the unsigned 32-bit value data allows me to manupilate all the colors in one chunk, or individually.

Hope that helped,
Thek

Share this post


Link to post
Share on other sites
There''s a couple of situations where you should use "friend":

* operator overloading, where the lhs is not the class-type so the function needs to be global rather than a member function.

* when two classes are EXTREMELY tightly coupled, such that both are maintained together. A good example of this is a class responsible for creating objects of another particular class.


"Sneftel is correct, if rather vulgar." --Flarelocke

Share this post


Link to post
Share on other sites
quote:
Original post by Sneftel
* when two classes are EXTREMELY tightly coupled, such that both are maintained together. A good example of this is a class responsible for creating objects of another particular class.



Or a container and its iterators.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan (C programming language co-inventor)

Share this post


Link to post
Share on other sites
probably best to use inner classes for container and its iterators, I think that implies automatic friendship (at least 1 way friendship)

Share this post


Link to post
Share on other sites
I thought so too, but apparently it doesn''t. (tho GCC allows it.) Which means one of these days I''m really going to have to go back through my code and fix stuff.


"Sneftel is correct, if rather vulgar." --Flarelocke

Share this post


Link to post
Share on other sites
an union uses less memory since all of its components uses the same memory space, but can''t be use as a struct replacement because changing any of the union "members" will change the others
ex.

union BytesOf {
int val;
char bytes[sizeof (int)];
};


after you set a value to val you can access it bytes by using .bytes[0] .bytes[1] ... .bytes[sizeof (int) - 1], changing any of the elements in the array would change the actual value of .val

Share this post


Link to post
Share on other sites
What if you unioned a float and an unsigned long ( I'm assuming they're both 32 bit )? Can you use that to get the bit representation of a float in an unsigned long as an integer without the indirection of a *( (unsigned long*)&my_float_val )?

As for friend, I usually just use small, inline accessor functions and let the compiler strip out the overhead instead of bothering with friending.

[edited by - me22 on March 7, 2004 6:08:16 PM]

Share this post


Link to post
Share on other sites
So inner classes don''t have access to private members of the owner class!? That''s weird...

oh, what happens if you put an unsigned long and a signed long in a union? does that make the union "sign" safe?

Share this post


Link to post
Share on other sites
quote:
Original post by me22
Can you use that to get the bit representation of a float in an unsigned long as an integer without the indirection of a *( (unsigned long*)&my_float_val )?

Yup. For D3D it''s one of the handier places to use a union.

Share this post


Link to post
Share on other sites
quote:
Original post by DevLiquidKnight
I was wondering when do you use the "friend" statement or whatever in C++. Also when is it best to use a union?

My friend said unions were faster then a typedef struct because he said they are in the same location in memory, which makes them not use as much memory or something. Im not sure if this is right. Im also not sure when to use it if anyone can give me an idea of when id use it?

I also was wondering when you use a friend data type or object type and what type of things it will allow me to do that I can not do normally?


Friend is a cheating way around some OO rules. Use sparingly - lots of posters seem to have posted good links about this already. Particularly useful for operator overloading.

Union - useful for creating something equivalent to a visual basic variant - and COM uses the mechanism to do something like that. Like friend though, it''s use should be sparingly. Optimisation of this sort should really only be bothered with if a. You are a flash git looking for trouble or b. you are working with very limited memory. Nowadays for PC programmers (and in the gaming domain) unless your union is doing something very very generic (buffer for network comms perhaps?) you are probably a.

Share this post


Link to post
Share on other sites
I''d use unions when I wanted to view the same data in different ways (the rgba color is a perfect example of this).

An example of friend usage would be making an overloaded operator to multiply a vector3 object by a matrix3x3 object. At least I think that''s a good example - never actually used it in real life!

Share this post


Link to post
Share on other sites

  • Advertisement