Defining a container class using a vector

S

samumatt

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!
 
M

maverik

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.
 
S

samumatt

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...
 
S

samumatt

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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top