• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Feb 01, 2015 at 06:53 PM by Glurth for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Glurth · Jan 07, 2015 at 06:00 PM · classinheritanceconstructor

How do I make all child-classes inherit all constructors in the parent class, automatically?

 class Animal{
     public Animal(){... blah blah…}
     public Animal(string name){ ... blah blah…}
 }
 class Cat:Animal{
     public Cat(string name):base(name){}
 }
 class Dog:Animal{
     public Dog(string name):base(name){}
 }

…

 Animal a=new Cat(“cat”);

…

This seems like a real pain, if I have a bunch of different constructors and a bunch of child-classes. Why aren't those completely EMPTY Cat & Dog constructors, that ONLY call the base class constructor, automatically IMPLIED, unless specified otherwise? It seems like, only the default constructor, without any parameters, is automatically inherited like this (unless specified otherwise).

Main Question: Is there some syntax to tell the compiler do this?

(If I don't define that Cat constructor manually, the compiler generates this error: ‘Cat does not contain a constructor that takes 1 argument’ on the line: Animal a=new Cat(“cat”); )

I know a lot of thought went into c#, so perhaps there is something I am not considering, that would make this a bad idea?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Glurth · Jan 07, 2015 at 06:19 PM 0
Share

Gosh I'm awful at searching. Thanks Dan, that was exactly what I needed. Please upgrade your comment to an answer, so I can accept it as such.

avatar image Victory Dan Greene · Jan 07, 2015 at 06:21 PM 0
Share

Sure thing, sorry it's not better news!

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by Victory Dan Greene · Jan 07, 2015 at 06:12 PM

There's a StackOverflow thread on this. Tl;dr: C# just doesn't work that way.

I think the rationale was that any given class's constructors should be identifiable in the class signature.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Class transform "null" keyword error. 1 Answer

How to "include" an inherited C# Class into the file? 3 Answers

How to build up the class structure for an object 1 Answer

Multi inheritance in Boo 2 Answers

Private variable is not saved 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges