Sign in to follow this  
Westones3

Unity Inherited Class Constructor/Code Cleanup

Recommended Posts

Westones3    122
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
deffer    754
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
Westones3    122
That's what I was afraid of. Thank you for the reply Deffer.

Edit: Oops, I posted this thread in the wrong forum. I meant to post it in the General section, sorry.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
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
leiavoia    960
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  

  • Similar Content

    • By ilovegames
      The legendary UAZ SUV is off in an unprecedented expedition on the picturesque,
      yet rugged regions of Russia. Powerful domestic cross-country vehicles have to go through many trials in the expanses of the country! There are three game modes (Exploring the world, Delivery and Checkpoints).
      Explore the vastness of Russia, perform tasks, and feel the power of the legendary UAZ!

      Features:
      - Large Map.
      - Diverse terrain (mountains, villages, fields, etc.)
      - Modern graphics and effects.
      - Pleasant music.
      - 11 types of camera views.
      - Different weather conditions (fog, rain, etc.)
      - Game modes (Free Driving, Delivery & Checkpoints)
      - 3 models of the legendary brand.
      - And much more!
      Download https://falcoware.com/UazOffRoadNewHorizon.php




    • By ilovegames
      Arcade game in black and white style. In it you will need to drive a motorcycle through a variety of challenging trails. Come accessible trails and open new ones. All in the game of their 21 game features realistic physics, and you will need to acquire the skill that would deal with it.
      Download https://falcoware.com/LostTrack.php
       



    • By mmortall
      Genre: Story Puzzle Technologies: Unity Web Player, C# Mono, Objetive-C Platforms: iOS   Language: English, Russian   Websites: App Store www.welldonegame.com Facebook group   Overview: Puzzle with love story. You need to solve mazes to reach girl you love in a beautiful environment. 80+ completely diverse stages, that show the history of Boy’s and Girl’s relationship AR mode (for iOS 11) Procedure generated nonrepetitive mazes Infinite mode with leaderboards Collecting unique postcards   Slogan: Love is a maze   Video   Screenshots   Needs your feedbacks
    • By WinterDragon
      My first goal is to build in python a text-based (next version will have graphics) adventure game scenario where a player enters a tavern, and goes on a quest. Earns money, buys weapons and gets in a fight.
       
      So I've got four ideas for games I want to build.
      1. a space simulation where a mute evolving clique is tasked with colonising planets - it's also a social network.
      My next goal is to build a 3rd person shooter and streets of rage style framework for a action/adventure game based on my story which is about God's mightiest heroes (with mutation sandbox) versus the devil (read pinhead meets bizarro superman).
      I also want to build in python, a 3d isometric rpg/sim that looks like the sims 1 but thematically is more like Bully or GTA1, but plays like Hell's Kitchen DS combined with Dofus, with environments combining elements of Phantasy Star, Sonic and art photography of convenience stores, etc.
      I also want to build an epic adventure story in RPGMakerMV.
      Any advice as to what else I should build in between to get closer to my goal projects.
       
      In a few weeks I'll have my first game, I'm not really sure how to turn it into an executable file so i can share it with people who don't have python installed.
    • By Steve Vermeulen
      I've been using async-await in Unity 2017 for awhile now and went through some hurdles getting it to work properly with the unity way of doing things. In order to avoid anyone else suffering the same problems I've posted a tutorial with what I've learned here:

      http://www.stevevermeulen.com/index.php/2017/09/23/using-async-await-in-unity3d-2017/
  • Popular Now