Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

FRIENDS, STATIC, GLOBALS

Name: Anonymous 2010-11-09 13:59

these 3 things i've been told to never use, Globals I can kinda understand, but static and friends...? wtf

Name: Anonymous 2010-11-10 20:47

>>17
sorry, i forgot. but basically, "friend" allows functions outside the class access it's privates and protected stuff, that's why people bitch about it: example
class CSquare;
 
class CRectangle {
    int width, height;
  public:
    int area (void)
      {return (width * height);}
    void convert (CSquare a);
};
 
class CSquare {
  private:
    int side;
  public:
    void set_side (int a)
      {side=a;}
    friend class CRectangle;
};
 
void CRectangle::convert (CSquare a) {
  width = a.side;
  height = a.side;
}
 
int main () {
  CSquare sqr;
  CRectangle rect;
  sqr.set_side(4);
  rect.convert(sqr);
  cout << rect.area();
  return 0;
}


here, CRectangle can access the "private int side" from CSquare because CRectangle was declared friend of CSquare.
That's the basic notion of "class friendship"

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List