[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Defining a container class using a vector

samumatt@gmail.com

11/24/2008 11:25:00 AM

Hi all. I'm writing a board game for a University project, but i'm in
trouble on the definition of the Board class

i have class Square (which defines every square of the board by some
properties) and now i need a container class Board

class Board{
private:
int length;
vector<Square*> B;
public:
Board(): B(length){};
//~Board();
void setSize(int);
int Begin();
int End();
int moveFW(ints);
};

this is my board.h: setSize let you set the length of the board
(number of squares) and inside the Board() constructor, i define a
vector B of square* (so i can create them dinamically with the square
constructor)
now i need to define the Begin method (returning the first element of
the board), the End method (the last element) and the moveFW method
(in other words, my Board class iterator), but i'm not sure if i can
index directly to my vector B or i need something else
any hint?
thanks!
3 Answers

maverik

11/24/2008 11:48:00 AM

0

On Nov 24, 2:25 pm, "samum...@gmail.com" <samum...@gmail.com> wrote:
> Hi all. I'm writing a board game for a University project, but i'm in
> trouble on the definition of the Board class
>
> i have class Square (which defines every square of the board by some
> properties) and now i need a container class Board
>
> class Board{
> private:
> int length;
> vector<Square*> B;
> public:
> Board(): B(length){};
> //~Board();
> void setSize(int);
> int Begin();
> int End();
> int moveFW(ints);
>
> };
>
> this is my board.h: setSize let you set the length of the board
> (number of squares) and inside the Board() constructor, i define a
> vector B of square*

Emm. If you mean

> Board(): B(length){};

then it is not inside constructor, AFAIK it called "initialization
list".

> now i need to define the Begin method (returning the first element of
> the board), the End method (the last element)

Why return value of Begin(), End() is int. As I understand you it
should be Square* if you want to return first / last value.

In case of iterators you can try:

typedef std::vector<Square*>::iterator iterator;
typedef std::vector<Square*>::const_iterator const_iterator;

iterator Begin();
const_iterator Begin() const;

iterator End();
const_iterator End() const;

Correct me if I am wrong, cause I'm not sure about that trick.

samumatt@gmail.com

11/24/2008 1:04:00 PM

0

yep i mistyped :P the return of Begin and End is a square* and i could
return direcly the index of the begin and the end (B[0] or B[size-1])
without using an iterator...

samumatt@gmail.com

11/24/2008 1:05:00 PM

0

yep i mistyped :P the return of Begin and End is a square* and i could
return direcly the index of the begin and the end (B[0] or B[size-1])
without using an iterator...