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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:55:44+00:00 2026-06-06T18:55:44+00:00

Inspired by this question , I’d like to know whether there is any trick

  • 0

Inspired by this question, I’d like to know whether there is any trick (like SFINAE) to achieve the opposite.

Edit: Adding example

I have something like this (it is very simplified because it has a lot of details):

#include <iostream>

class Base {
public:
  Base(){}
  virtual ~Base(){
  }
  void initialize() { implementation(); }
private:
  virtual void implementation() = 0;
};

class Derived : public Base {
private:
  virtual void implementation() { std::cout << "initialization" << std::endl;}
};

int main() {

  std::unique_ptr<Base> b(new Derived());
  b->initialize();

  return 0;
}

The initialize method must be called.

But if I put initialize method inside Base constructor it will call a pure virtual method.
I want to know if there is a way to prevent someone to use improperly the constructed object.

Edit: My “solution”:

#include <iostream>

class Base {
public:
  virtual ~Base(){
  }
  Base() {};
private:
  void initialize() { implementation(); }
  virtual void implementation() = 0;
  template<class DerivedType> friend Base *factory();
};

class Derived : public Base {
private:
  Derived() {}
  virtual void implementation() { std::cout << "initialization" << std::endl;}
  template<class DerivedType> friend Base *factory();
};

template<class DerivedType>
static Base *factory(){
  Base *b = new DerivedType();
  b->initialize();
  return b;
}

int main() {

  std::unique_ptr<Base> b(factory<Derived>());

  return 0;
}
  • 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-06T18:55:46+00:00Added an answer on June 6, 2026 at 6:55 pm

    You can’t force a compiler error, but there are other alternatives.

    If an instance of your type is simply invalid unless initialize is called, and if documenting that fact or throwing an exception upon use if uninitialized is unacceptable, then you should make the constructor private and expose a static method to create instances of your type.

    This way clients of your code simply cannot use an uninitialized instance of your type. If there is a better, more elegant approach I don’t know of it (but maybe someone around here does).

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

Sidebar

Related Questions

This question is inspired by this question . I'd like to get a dictionary
Inspired by this question. Why is there no list.clear() method in python? I've found
This is directly inspired by this question . There are numerous references/statements that bitwise
Inspired by this question where there are differing views on SET NOCOUNT... Should we
This question is inspired by this recent outage: https://status.heroku.com/incident/212 There doesn't seem to be
This question is inspired by Jon Skeet's answer: Is there a c# equivalent to
inspired by this question since i do not find any good sql casts out
This question was inspired by one I asked almost a year ago - any-orms-that-work-with-ms-access-for-prototyping
This question was inspired by a similar question: How does delete[] know the size
This question is inspired by the article Why are Facebook, Digg, and Twitter so

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.