Question

I'm trying to compile such code:

#include <iostream>
using namespace std;

class CPosition
{
  private:
    int itsX,itsY;
  public:
    void Show();
    void Set(int,int);
};

void CPosition::Set(int a, int b)
{
  itsX=a;
  itsY=b;
}

void CPosition::Show()
{
    cout << "x:" << itsX << " y:" << itsY << endl;
}

class CCube
{
  friend class CPosition;
  private:
         CPosition Position;
};

main()
{
  CCube cube1;

  cube1.Position.Show();
  cube1.Position.Set(2,3);
  cube1.Position.Show();
}

but get 'CCube::Position' is not accessible in function main() 3 times. I want class CPosition to be declared outside CCube so that I can use it in future in new classes e.g. CBall :) but how can I make it work without using inheritance. Is it possible :)?

Regards, PK

Was it helpful?

Solution

In addition to the normal getter you should also have a const getter.
Please note the return by reference. This allows you any call to SetXX() to affect the copy of Position inside CCube and not the copy that you have been updating.

class CCube
{
    private:
        CPosition Position;
    public:
        CPosition&       getPosition()       { return Position; }
        CPosition const& getPosition() const { return Position; }
};

OTHER TIPS

The statement friend class CPosition; means that CPosition can now access the private members of the CCube class. To every other class the members are still as private as you declared them. To make the sample work you'd:

class CCube
{
     public:
         CPosition Position;
};

errr, no, Position isnt visible in the function "main"

Make it public... or put a public getter function in

Your friend declaration would need to be

friend int main();

You are giving 'main' permission to access CCube's private member.

Well, you already got an answer for the error. One things though: Do you plan on having the cube access the private members of CPosition (itsX, itsY)? If not, it doesn't need to be declared as a friend. If it is, consider exposing public methods in CPosition to return X and Y, and you still don't need to declare it as a friend.

What I'm saying is that you need to make sure that you need to use friends. Here's a good place to start.

OK it works with:

class CCube
{
  private:
         CPosition Position;
  public:
  CPosition& getPosition() { return Position; }
};

main()
{
  CCube cube1;

  cube1.getPosition().Show();
  cube1.getPosition().Set(2,3);
  cube1.getPosition().Show();
}

Thanks

You have 3 questions in one:

  1. reusability of CPosition
  2. how to influence a shape's state (in this case includes a member of type CPosition, but may also include an int radius or something else)
  3. Usage of the friend keyword

You clearly say you need the CPosition class to be accessible from other places. Well, it is class. Problem 1 is solved.

If you want you CCube users to be able to change a CCube's position, you need to provide a means for that:

  1. By making CCube::Position public
  2. By adding accessor methods - CCube::MoveTo( const CPosition& p ) and CCube::GetPosition() const.

As @Firas said: don't play with friend until you're really sure you need it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top