delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/03/26/05:53:56

From: md2828 AT mclink DOT it (Marco Salvalaggio)
To: "Lee Simons" <lee AT dialin DOT co DOT uk>
Cc: djgpp AT delorie DOT com
Subject: Re: C++ Class Help
Date: Wed, 26 Mar 1997 10:38:15 GMT
Message-ID: <3339fc8f.5600527@newmail.mclink.it>
References: <199703252129 DOT VAA02177 AT post DOT dialin DOT co DOT uk>
In-Reply-To: <199703252129.VAA02177@post.dialin.co.uk>
MIME-Version: 1.0

On Tue, 25 Mar 1997 19:44:48 -0000, you wrote:

>Hi,
>
>I'm writing a football game, and as in real life football there are lots of
>players.
>I'm going to create and object array of a class Player.  This class will
>hold information about the players, such as skill, speed etc.  It will also
>hold the players position in xyz space and it's personal functions, for
>telling the program what its skill is, shooting, passing, and player AI.
>
>I want to declare the array, like so:
>
>Player data[1000]; // 1000 players in data array =)
>
>But, the problem is, I don't want to use loads of functions to set the
>player's data. Like:
>
>data[0].SetSkill(100);
>data[0].SetSpeed(99);
>
>etc..
>
>I would like to be able to do a:
>
>data[0](100, 99)
>
>sort of thing.  Where 100 is the skill as shown in the other example, and
>99 is the speed.
>
>Thanks a lot in advance.
>
>Lee
>

Overload the () operator. Example follow:

#include <iostream.h>

class Test
{
   int x,y;
public:
   void operator() (int, int);
   void disp() { cout << endl << "x: " << x << " y: " << y; }
};

void Test::operator() (int a, int b)
{
   x = a;
   y = b;
}

int main()
{
   Test array[5];

   array[0](1,2);
   array[1](3,4);
   array[2](5,6);
   array[3](7,8);
   array[4](9,10);

   for(int i = 0; i < 5; ++i)
      array[i].disp();

   return 0;
}

HTH, Marco.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019