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 6799351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:50:17+00:00 2026-05-26T18:50:17+00:00

While working with templates I ran into a need to make a base class

  • 0

While working with templates I ran into a need to make a base class constructors accessible from inherited classes for object creation to decrease copy/paste operations.
I was thinking to do this through using keyword in same manner with functions case, but that not work.

class A
{
public: 
    A(int val) {}
};

class B : public A
{
};

class C : public A
{
public:
    C(const string &val) {}
};

class D : public A
{
public:
    D(const string &val) {}
    using A::A;              // g++ error: A::A names constructor
};

void main()
{
    B b(10);                // Ok.   (A::A constructor is not overlapped)
    C c(10);                // error: no matching function to call to 'C::C(int)'
}

So my question: Is there any way to import a base class constructors after new ones in inherited class been declared?

Or there is only one alternative to declare new constructors and call a base ones from initializer list?

  • 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-26T18:50:17+00:00Added an answer on May 26, 2026 at 6:50 pm

    Prefer initialization:

    class C : public A
    {
    public:
        C(const string &val) : A(anInt) {}
    };
    

    In C++11, you can use inheriting constructors (which has the syntax seen in your example D).

    Update: Inheriting Constructors have been available in GCC since version 4.8.


    If you don’t find initialization appealing (e.g. due to the number of possibilities in your actual case), then you might favor this approach for some TMP constructs:

    class A
    {
    public: 
        A() {}
        virtual ~A() {}
        void init(int) { std::cout << "A\n"; }
    };
    
    class B : public A
    {
    public:
        B() : A() {}
        void init(int) { std::cout << "B\n"; }
    };
    
    class C : public A
    {
    public:
        C() : A() {}
        void init(int) { std::cout << "C\n"; }
    };
    
    class D : public A
    {
    public:
        D() : A() {}
        using A::init;
        void init(const std::string& s) { std::cout << "D -> " << s << "\n"; }
    };
    
    int main()
    {
        B b; b.init(10);
        C c; c.init(10);
        D d; d.init(10); d.init("a");
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While working with some templates and writing myself a basic container class with iterators,
While working on my own type erasure iterator, I ran into an issue where
i have faced some problem while navigating from one class to another with the
Its been a while since i am working with the AJAX and getting templates
While working in WPF i have the need for a Dynamic Grid. By this
While working in a Java app, I recently needed to assemble a comma-delimited list
While working between a Windows MySQL server and a Debian MySQL server, I noticed
While working on a project, I came across a JS-script created by a former
While working on a C++ project, I was looking for a third party library
While working on an existing project I suddenly got the following error when trying

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.