Sign in to follow this  
Westones3

Unity Inherited Class Constructor/Code Cleanup

Recommended Posts

I have a small question/problem. I have been working on a project that uses a derived class whos parent class has a rather large constructor. The inherited class is intended to contain the same paramaters in its constructor as the parent class. Here is a very brief sample of my code format:
class Base{
  public:
     Base(int a, int b, int c) //The actual constructor is much larger
};

class Derived : Base{
  public:
     Derived(int a, int b, int c) : Base(a,b,c){};
};

So my question is pretty simple but I was having a little trouble finding a definitive answer through searches, books, articles etc. I understand that constructors are not inherited, but is there any way around having to write in all that extra functionality for the Derived class? Thanks in advance for any and all help, this community is awsome!

Share this post


Link to post
Share on other sites
Unfortunately, there isn't. It's been one of many reasons of programmers cursing on C++.

But, to my knowledge, introducing contructor inheritance is being reviewed by C++ standard comitee (although I don't believe they will go through with it).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Westones3
I understand that constructors are not inherited, but is there any way around having to write in all that extra functionality for the Derived class?

you could use a template or even a macro to avoid having to type everything again :-)
However seriously: if you have any sort of significant initialization in your constructor, you may want to consider splitting its functionality into a separate init() function, that you simply call from your constructor.


Share this post


Link to post
Share on other sites
A few things you can do, one already mentioned:

1) have an init() / reset() function. It doesn't get you around having to type in params, but it does delay it ;-)

2) defaults, in two flavors: internal and parameter defaults. Parameter defaults will save you some typing:

Thing::Thing( a=0, b=0, c=0 );

the other is just internal initialization:

Thing::Thing() {
// set myself up automatically.
// wait for user to override any defaults though other functions.
}

But other than that, get used to typing!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Announcements

  • Forum Statistics

    • Total Topics
      628346
    • Total Posts
      2982202
  • Similar Content

    • By koto
      Hello,

      I'm developing a roguelike game. Currently it's in a playable state and I think it's a high time to show it and hopefully get some feedback.

      The game has following features:
      - turn based
      - procedurally generated levels
      - tons of loot
      - spells system
      - trading system
      - some quests
      - some limited crafting
      - door/key/lever mechanics
      - quite a lot of different (look/behaviour) monsters

      Trailer:
      Demo: http://kotogames.com/wp-content/uploads/2017/11/OuaD_demo.zip
      Regards,
      Tom
       
    • By Afambus
      Hello,
      I have a full experienced team that looking for a composer and another 3d designer, im expecting as much from you there are other people in this server that can help you we have a determined team, we have done many things in our game there is more information in the discord about everyone and there skills there region and about the game, if you join the discord pm asap
      https://discord.gg/b2teN3m
    • By BAG Labs
      Mobile SoS

      Platform: Android
      Genre: Board
      Link: Google Play
       
      This games sharpen memory and test your strategies to place S-O-S pattern within time limit and serve 3 difficulties as Easy, Normal, and Hard.

      Goals of the game is to put S-O-S words in patterns (Horizontal, Vertical, and Diagonal) alternately with enemy.
       
      Features:
      Single Player Multiplayer Achievements Leaderboards  
       
      Screenshot:

       

       

       
      Link: Google Play
       
      Please help us improve this game with review
    • By abarnes
      Hello!
      I am a game development student in my second year of a three year program and I would like to start building my portfolio. I was thinking of creating some games to show what I can do to potential employers since I wont have any work related experience when I graduate. But as I'm sure you all know there are tons of ways to approach developing/designing a game and I'm curious if anyone had any insight as to any "standards" that come with this? Is it okay to use game engines like Unity, Unreal, Game Maker etc? Or would it be better to make a game from scratch to better show case your skills? Any and all advice will be greatly appreciated!
    • By Hilster
      Hello 2D Artists,
      I've started making a 2D Puzzle Adventure game for mobile and I'm looking for someone who would want in on creating assets for the game. The core of the programming is pretty much complete, you can walk within the grid laid out and push boxes, when there is an object on top of a pressure pad it will activate the linked objects or if there is one object with multiple linked pressure pads it requires you to activate all points for the object to become active. 

      The level iteration for the game is quick and simple, a Photoshop file that is made of individual pixels that represents objects is put into the game and it creates the level out of those pixels with the assigned objects.
      The objects that need sprites created so far is the character, box, pressure pad, door, trap door, the walls, the stairs and the tiled background.
      I intend to add more objects so the amount I'd like to add will be extended.
      My motivations for posting here is to have something that looks nice to be able to display on my portfolio, so if you're looking for a working game that you can place your art into and improve the look of your portfolio then we're in business.
      Please reply with a few past examples of your art below and I'll be in touch!
  • Popular Now