Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 555377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:48:30+00:00 2026-05-13T11:48:30+00:00

I have a Base Class with two constructors, requiring a parameter: public abstract class

  • 0

I have a Base Class with two constructors, requiring a parameter:

public abstract class StoreBase 
{
    private readonly SomeObject_sobj;

    protected StoreBase(SomeObject sobj)
    {
        _sobj = sobj;
    }

    protected StoreBase(OtherObject oobj)
    {
        _sobj = new SomeObject(oobj);
    }
}

Then I have a derived class:

public class MyDerived: StoreBase
{

}

This causes a compilation error as base class doesn't contain parameterless constructor.

My understanding is that because MyDerived doesn’t contain a constructor, the compiler adds a parameterless constructor (that’s well known and nothing to do with derived classes). However, as it derives from another class, the base class constructor needs to run first, and there is no way to determine which constructor should run from the empty MyDerived constructor.

Basically I’m asking: can I avoid copy/pasting all constructors from Base into Derived class if I really don’t need additional constructor logic? Can I say “Take all constructors from base” without adding them all?

(And yes, I know I could/should refactor this into a parameterless constructor and a protected virtual Initialize() method. but I still wonder if I can work with constructors and still avoid copy/paste)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T11:48:30+00:00Added an answer on May 13, 2026 at 11:48 am

    No – you will need to implement the (appropriate) constructors in the derived class, as well.

    The derived class only needs to use one of the base constructors – so the constructors required in it may be completely different than the base class. They will need to be implemented by hand, even if that’s just:

    public class MyDerived : StoreBase
    {
         public MyDerived(SomeObject sobj) : base(sobj) {}
         public MyDerived(OtherObject  oobj) : base(oobj) {}
    }
    

    Also:

    (And yes, I know I could/should refactor this into a parameterless constructor and a protected virtual Initialize() method. but I still wonder if I can work with constructors and still avoid copy/paste)

    Although I see this touted, I believe this is not always a good practice. In many cases, this is actually problematic, as you’re relying on the subclass to properly call Initialize if they override your protected virtual method. For example, if the subclass did this, it could potentially be very bad:

    public class MyDerived : StoreBase
    {
       // .. other stuff
       protected override void Initialize()
       {
           // Leave out, intentionally or accidentally, the following:
           // base.Initialize(); 
       }
    }
    

    I actually avoid this in most situations, and initialize in the constructors (or in a private, non-virtual initialize method). Not doing this breaks any guarantees you have that your initialization will always occur the way you intend.

    Constructors and constructor chaining provide the same functionality, with much better guarantees.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method and two classes defined like this: template<template<class X> class T>
Here is an example. I have two classes, one inherited, and both have a
#include<iostream> using namespace std; class A { public: int i; A() {cout<<A()<<endl;} ~A() {cout<<~A()<<endl;}
TL;DR: How can I consolidate logic shared by two custom ModelBinder implementations into a
I have a question about some C++ standard compliance or lack of it. In
I have a problem regarding the use of interfaces vs concrete classes. I have
First of all, I want to make myself clear that I do understand that
So, I only realised today that __new__ is deprecated for receiving arguments, as of
I'm looking to implement my own set of Exceptions for a projects I am
So I implemented my own form of Enum static classes so I can associate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.