# Constructor for classes. Help!

## Recommended Posts

Hello, I have a quick question on the constructors of a class. For example... I have created a map class and initialized a new map. Map newMap("Map Name", sizeOfMap, playerStart, item1, item2); So i declare item 1, however i dont want there to be an item2 on the map. Do i just leave it like ... Map newMap("Map Name", sizeOfMap, playerStart, item1, ); leave it blank or put false? Thanks for any help =)

##### Share on other sites
Define a constructor which does not require an "item2" argument, and use that constructor.

Map(const std::string &name,    dimensions_type dims,    position_type player_start,    item1_type item1) /* constructor code here */Map newMap("Map name", sizeofMap, playerStart, item1);

##### Share on other sites
Why not take a container of items, which will have a variable size? Or can there only be two items in a map? [grin]

##### Share on other sites
Ahh thanks, I ws wondering about overlaoding the constructor.

##### Share on other sites
If there is max two items you can use default parameters

Map(const std::string &name,    dimensions_type dims,    position_type player_start,    item1_type item1,    item1_type item2=0) /* constructor code here */Map newMap("Map name", sizeofMap, playerStart, item1);

(Code modified from the one posted by ToohrVyk)

If there is more I recommend using, as rip-off said, a container.

##### Share on other sites
Quote:
 Original post by ZaHgOIf there is max two items you can use default parameters
That would be assuming that 'zero' is a valid default value for an item2_type, and that adding a 'zero' item2 is equivalent to adding no item2 at all. Most of the time, items don't have a neutral default value that you can use.

##### Share on other sites
I am somewhat new to C++ so what i am doing could be totally wrong :P

But i was thinking of making the map using an array: map[25]

so i think if i were to set item2 to 0, it would then be placed in map[0] even though i dont want it to be created.

What about making the items a bool?
Map newMap("NewMap", 25, 0, true, false);

Again this could be TOTALLY wrong =P

if ((gameMap.name == "NewMap") && (gameMap.Item2 == true))
{
//Put the item in map[3]
map[3] = define item
}

Would this work?

##### Share on other sites
Quote:
 Original post by ultigmaso i think if i were to set item2 to 0, it would then be placed in map[0] even though i dont want it to be created.
Constructors don't fill arrays on their own, they only do this if you tell them to. So, if you didn't tell your constructor to place something in map[0] when item2 is 0, it won't do that.

Quote:
 What about making the items a bool?Map newMap("NewMap", 25, 0, true, false);
Your arguments are what they are. You can't go around changing their types as if it didn't matter.

As a general rule, you should be able to explain what every function you write does (including constructors), down to the precise details of what its arguments are used for, what exceptions it can throw, and what it returns (if anything). If you can't provide a complete and accurate description of all the above, throw your code away (code you don't understand is code you can't use), decide on a new function and what it should do, and write it.

So, applying this to your map constructor, what do the map arguments represent, in "human" terms?

##### Share on other sites
What do the map arguments represent, in "human" terms?

The Map arguments in this case are,
Map(std::string mName, int mMapSize, int mStartPos, int mShop, int mItem1,
int mItem2, int mItem3, int mBoss, int mExit1, int mExit2);

I know what each of them mean, but I forgot that they just contain numbers ect.

After it has been initialised, I then have to store thier values into the array.

Would i be right in saying

Map startMap("First Map", 25, 0, 20, 19, 4, 0, 0, 0, 24, 0);//Instantise the map
if (startMap.mItem1 != 0 )
{
board[startMap.mItem1] = new item;
}
else
cary on

so that if player landed on board[4] he would aquire the new item?

I'm not sure if this is efficiant or not, but this is prob how i would tackle it as of now =P

Does this look acceptable?

##### Share on other sites
Quote:
 Original post by ultigmaWhat do the map arguments represent, in "human" terms?The Map arguments in this case are, Map(std::string mName, int mMapSize, int mStartPos, int mShop, int mItem1, int mItem2, int mItem3, int mBoss, int mExit1, int mExit2);I know what each of them mean, but I forgot that they just contain numbers ect.

Well, I don't know what these mean. Besides, what you showed was the arguments in "computer" terms (that is, the names and types). What is missing is the real signification: what does the mItem1 integer represent? Is it the number of items of the first type? Is it the position of the first item? Is it the identifier of the first item type to be used? Is it the priority associated with the first item? Something else?

As it stands now, it's difficult to understand what your constructor does, which in itself is a good hint that it should be changed to something more expressive. If you explain the purpose of the arguments, we may be able to provide a more acceptable alternative.

##### Share on other sites
Ahhh i see, the arguments are where i want them to be located on the map. So item1 would be positioned at board[4], and shop at board[20]

+ + + + + +
|strt| 01 | 02 | 03 |item|
+ + + + + +
| 05 | 06 | 07 | 08 | 09 |
+ + + + + +
| 10 | 11 | 12 | 13 | 14 |
+ + + + + +
| 15 | 16 | 17 | 18 | 19 |
+ + + + + +
|shop| 21 | 22 | 23 |exit|
+ + + + + +

hope this is more clear now =)

##### Share on other sites
So, you are therefore implying that a map will always contain one item1, one item2, one item3, one boss, one exit1, and one exit2 ? Isn't that contradictory with your first post? Also, can't your map contain more than three items, more than two exits, and more than one boss?

I would suggest instead using a list of items, a list of bosses, and a list of exits. These lists may contain any number, including zero, one or more.

Also, it's pretty obvious that your map is two-dimensional, so why are coordinates integers? Even if you still implement your positions as integers, you can always:

typedef int position;inline position at(int x, int y) { return x + y * width; }

##### Share on other sites

But i think you said something about, give them a null value, so i would give item2 and mItem3 a value of 0 then do an if statement

if(firstMap.mItem2 != 0)
{
board[mItem2] = new item;
}
else
{}

I suppose i am limiting myself by explicitly declaring that many items, exits and bosses.

But i am unsure of how i would create a list.
I know i'd need a vector.

Map(name, size, startPos, ..., vector <int> Item, vector <int> Exit,
vector <int> Boss);

Map startMap("Start Map", 25, 0, ..., 2, 1, 0);

how can i decide on the size of the array AND give the elements of the array values in the initialization of the map?

##### Share on other sites
The item/bosses/exit data should probably be in an external file. You can load the data into your containers, and pass the newly created containers to the map.

##### Share on other sites
Quote:
 Original post by ultigmaBut i think you said something about, give them a null value, so i would give item2 and mItem3 a value of 0 then do an if statement

If so, how would you differentiate between an element at the position 0, and an absent element? Don't use integers for everything. Learn to define new types, or use the built-in types;

Quote:
 how can i decide on the size of the array AND give the elements of the array values in the initialization of the map?
This is C++, a language known for its lack of expressiveness. You can't easily initialize a sequence of elements in standard C++, sadly. The good design solution would be to load the data from a file into a vector, and pass the vector to the map. Of course, this does not play well in things such as unit tests, where hard-coded data may be more easily obtained. Another alternative is therefore:

template<typename T, size_t N>size_t size(T (&)[N]) { return N; }dimensions dims(10,10);position items[] = { dims.at(0,0), dims.at(3,5), dims.at(5,2) };position exits[] = { dims.at(5,5) };position bosses[] = {};Map testMap("Test map", dims;            vector<position>(items, items + size(items)),             vector<position>(exits, exits + size(exits)),            vector<position>(bosses, bosses + size(bosses)));

Or, if using the boost library:
Map testMap("Test map", dims;            list_of(dims.at(0,0))(dims.at(3,5))(dims.at(5,2)).to_adapter(),            list_of(dims.at(1,1)).to_adapter(),            vector<position>());

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627657
• Total Posts
2978466

• 10
• 12
• 22
• 13
• 33