tags:

views:

60

answers:

3

I'm not sure how to call this. Basically I want to make a class with nested members.

Example:

ball->location->x;
or
ball->path->getPath();

right now I only know how to make public and private members such as

ball->x;
ball->findPath();

Thanks

+2  A: 

Something like this:

class Plass
{
public:
    Plass(Point *newPoint, Way *newWay)
    {
         moint = newPoint;
         bay = newWay;
         // or instantiate here:
         // moint = new Point();
         // bay = new Way();
         // just don't forget to mention it in destructor
    }
    Point *moint;
    Way *bay;
}

From here you can do:

Plass *doxy = new Plass();
doxy->moint->x;
doxy->bay->path->getPath();
alemjerus
You can use std::auto_ptr so you don't have to remember to delete the objects in destructor.
Messa
A: 

Try this:

struct Location
{
  int x;
  int y;
};

struct Path
{
  std::string getPath();
};

struct Ball
{
  Location location;
  Path path;
};

Ball ball;
ball.location.x;
ball.path.getPath();

Or if you must use the -> operator:

struct Location2
{
  int * x;
};

struct Ball2
{
  Location2 * location;
  Path *     path;
};

Ball2  ball;
ball->location->x;
ball->path->getPath();
Thomas Matthews
A: 
struct Location {
    int x, y;
};

struct Path {
    vector<Location> getPath();
};

struct Ball {
    Location location;
    Path path;
};

alternately

struct Ball {
    struct Location {
        int x, y;
    } location;
    struct Path {
        vector<Location> getPath();
    } path;
};
Potatoswatter