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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:49:12+00:00 2026-06-11T17:49:12+00:00

Since once cannot call a overwritten function of a derived class in base class

  • 0

Since once cannot call a overwritten function of a derived class in base class constructor, I would like to emulate this behavior (similar to what C#, Java, … are doing under the hood).

The most elegant I could come up with (from a signature side of view) is the following:

class Base {
protected:
    virtual void init() {
    }

    template <typename T, typename ...U>
    T internal_create(U&& u) {
        T instance(std::forward<U>(u)...);
        instance.init();
        return instance;
    }
};

class Derived : public Base {
protected:
    Derived() = default;
    virtual void init() override {
    }
public:
    static Derived create() {
        return internal_create<Derived>();
    }


};

Where create works as a substitute for the constructor (from public point of view) and the only way to instantiate an Object.
Question is whether this could be achieved simpler, since now every derived class has to implement create.

  • 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-06-11T17:49:13+00:00Added an answer on June 11, 2026 at 5:49 pm

    I can’t see that your code would compile. And I can’t see the point of the virtual init method there, although it does suggest two phase construction. Which is just evil, which you can find out more about by reading Bjarne Stroustrup’s appendix E to “The C++ Programming Language”, especially section E3.5.

    The usual methods for doing derived class initialization in a base class constructor are outlined in the FAQ. Since I once convinced Marshall to add that FAQ item, I feel free to also direct you to my own blog.

    In the future, remember that it is often a good idea to check out the FAQ first.


    Oh, I forgot. The point about the C++ rules for dynamic type during construction, is to provide type safe construction. In Java and C# you can easily introduce a bug where you’re accessing some uninitialized members of a derived class, but not in C++.

    Two phase construction ditches that type safety, and makes it difficult to write exception safe usage code, in return for the ability to code it up cleanly for an early 1990’s compiler (but really, who needs that ability).

    The other usual solutions are designed to keep the type safety.

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

Sidebar

Related Questions

I have this function, which is doing an asynchronous call to FB.api method. Now,
This has stumped me for a few hours now, since I cannot see any
Since I have no errors I don't know if this is the right place
I would like to generate a dynamic image from a script, and then have
Since I've not done this before I am not sure if the way I
I'm breaking my head since a whole day now but cannot find a solution,
Ok, I know how to call C++ method from QML, but in this case
Background I once wrote this method: private <T> SortedSet<T> createSortedSet() { return new TreeSet<T>();
(Since FBML and FBJS are deprecated by Facebook, I cannot use those approaches to
I've been trying to think since HOURS about this TopCoder problem and couldn't come

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.