Public Group

# Possible to change base class constructor parameters based on derived constructor parameter?

This topic is 2920 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm wonder for instance, if you have something like:

B::B( int n ): A( "NAME" )
{
}

Would it be possible, somehow maybe by changing a few small things up a bit, to change the string passed to the constructor of the base class A based on the value of n?
So that if a different variable is passed to B, its base class also gets different values... though not as simply as just passing the variable straight through.

Really, what I want to do is create a cash pick up in my 2D top down shooter. To create an item drop, a function is called which looks into a map keyed by the string name of that object and copies a prototype in the map into the new object.
My problem is... I want to have several kinds of cash pick up that are identical apart from value, and also the graphic displayed which is determined by the type field which determines the value etc.
So ideally, I want to just have one CashDrop class and use different parameters to create the different types rather than go making CashDrop100, CashDrop500 and so on, bloating out my program and making things quite irritating.

So my current idea is... give them different names based on their value, so "CASHDROP_100" etc. and then that can be looked up in the map. I suppose there are many other ways of doing it, but that seems the most tidy to me right now. Maybe I should refactor a little, but I would be interesting in seeing what the story is with this anyway.

##### Share on other sites
 std::string _map_n_to_string(int n) { std::string s = "boy"; switch (n) { case 6: s = "girl"; break; case 7: s = "neither"; break; } return s; } B::B(int n) : A(_map_n_to_string(n)) { } 

##### Share on other sites
Oh yeah... brilliantly simple. I should've thought of that but maybe it was tiredness from a few hours of programming.

Thanks

##### Share on other sites
You could use an enum for your different item drops instead of strings. Strings seem messy and too performance intensive for your purposes. Enums can also be used in more useful ways than a string.

It still seems like your design has a bit of a code smell in that you need to do this though. It feels like your function should take in both a type and some sort of value that signifies the quality of the drop in some way. Ex.

[source]
enum ItemTypes{
CASH_DROP_ENUMERATED_VALUE,
WEAPON_DROP_ENUMERATED_VALUE,
NUM_ITEM_TYPES
}

GenerateNewItemDrop(CASH_DROP_ENUMERATED_VALUE, 200);
GenerateNewItemDrop(WEAPON_DROP_ENUMERATED_VALUE, 5); // level 5 weapon or something
[/source]

I still feel like this system is too broad. I think I would rather see GenerateNewCashDrop(value) than GenerateNewItemDrop(ItemType, value).

##### Share on other sites
Yeah, I thought about adding some kind of variable into the function to create an ItemDrop itself, but then I decided that at this stage... where cash is the only kind that it seems will ever need it in this game, to go back and rejig things might be messy or more trouble than it's worth.

A different function for each type of ItemDrop to generate is definitely something to consider too... I suppose a single one has the advantage of being quicker to get going and perhaps more straightforward... but you're probably right in that, if my current project was more complicated or had derived classes of ItemDrop that were too complex or different to each other, it might indeed turn into a nightmare to try and keep a single function to instantiate them all.

Thanks for the input...

##### Share on other sites
One question about having distinct functions to generate different kinds of ItemDrop:
If you were in a situation where you couldn't know beforehand what type of ItemDrop you needed to create, e.g. an enemy has a drop table of all the items it can spawn upon death which can be of any type and you're iterating through it and spawning as appropriate, how would you approach selecting the right function?

I'm using XML for just that situation right now, and I suppose one idea would be to put the item's type in the XML element with it and then use a switch statement in the code but maybe there are cleaner ways.

##### Share on other sites

One question about having distinct functions to generate different kinds of ItemDrop:
If you were in a situation where you couldn't know beforehand what type of ItemDrop you needed to create, e.g. an enemy has a drop table of all the items it can spawn upon death which can be of any type and you're iterating through it and spawning as appropriate, how would you approach selecting the right function?

I'm using XML for just that situation right now, and I suppose one idea would be to put the item's type in the XML element with it and then use a switch statement in the code but maybe there are cleaner ways.

Array/Vector of possible item drops which might have random quality. Choose randomly from inside that array which type of item is dropped then randomly generate quality. Use this to call appropriate function.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 11
• 9
• 11
• 15