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

  • SEARCH
  • Home
  • 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 8494151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:13:16+00:00 2026-06-10T23:13:16+00:00

I have chicken<->egg problem. I want to create interface for a container classes, so

  • 0

I have chicken<->egg problem.
I want to create interface for a container classes, so that I can force implementation of specific attr-accessors and other methods and at the same time I want to be able to call those methods from the Containers constructors.
But as we know calling virtual method from Constructor will call the Base class methods, because of the sequence at which the constructors in hierarchy are called.
So the problem is I need virtual methods to force implementation, but that is the thing I can’t call in constructor.
EDIT: I want when I have to implement Container class (C1,C2…) to be forced to implement specific methods AND second I want to be able to use those methods in the Container-constructors. In simple terms I want to implement INTERFACE requirement, declaration-only-mixin. thanks
How do you solve this problem ?
Below is code sample.

#include <iostream>
using namespace std;

class Base {
public: 
    virtual void myvirt() { cout << "Base::virt()" << endl; };
};

class C1 : public Base {
 public:
    C1() {cout << "C1()" << endl; C1::myvirt(); };
    C1(int i) { cout << "C1(int)" << endl; }
    void myvirt() { cout << "C1::virt" << endl;}
};


class C2 : public Base {
 public:
    C2() {cout << "C2()" << endl; C2::myvirt();};
    C2(int i) { cout << "C2(int)" << endl; }
    void myvirt() { cout << "C2::virt" << endl;}
};

template<class C = C1>
class Sc: public C {
 public:
    Sc() : C() { cout << "Sc()" << endl; };
    Sc(int n): C(n)  { cout << "Sc(int)" << endl; };

};


int main() {
    Sc<C2>  $sx(12);
    return 0;
}

Just as I’m asking is there some way to force implementation in descendant classes of specific Constructors().
Something like “virtual Constructors”.

  • 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-10T23:13:18+00:00Added an answer on June 10, 2026 at 11:13 pm

    Just make the constructor trivial and then after the constructor has returned, call some kind of initialization function to complete the construction of the object. You can also use the clone idiom, depending on how you want to decide what kind of object to construct.

    You can also have a wrapper class whose constructor first constructs the inner class and then calls its virtual initialization function.

    Update: You can wrap like this:

     Class Wrapper : public Inner
     {
            public:
            Wrapper(...) : Inner(...)
            {
                init();
            }
            virtual ~Wrapper();
     }
    

    Now you can just do

     Wrapper foo(whatever);
    

    And you can use foo just like an Inner.

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

Sidebar

Related Questions

I have a list of Chicken s and Egg s I want to create.
I have a problem similar to the egg and the chicken. I have two
I have a chicken-egg problem. I would like too implement a system in PHP
I think I have a bit of a chicken and egg problem. I would
I heard about the chicken and the egg and bootstrapping. I have a few
I've got a chicken and egg problem here. Firstly I've got a userprofile class
For example i have two classes A and B, such that for two objects
let's assume, I have a model called Chicken: class Chicken(models.Model): name = models.CharField(max_length=30) and
have a problem. At first look at this HTML <div id=map style=background-image: url(map.png); width:
I have two enums that cross reference each other. Each one has a constructor

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.