Constructor for classes. Help!

Started by
13 comments, last by ToohrVyk 15 years, 8 months ago
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 =)
Advertisement
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);
Why not take a container of items, which will have a variable size? Or can there only be two items in a map? [grin]
Ahh thanks, I ws wondering about overlaoding the constructor.

Thanks for the reply =D
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.
Quote:Original post by ZaHgO
If 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.

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?
Quote:Original post by ultigma
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.
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?
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?
Quote:Original post by ultigma
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.


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.

This topic is closed to new replies.

Advertisement