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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:00:07+00:00 2026-06-09T04:00:07+00:00

can i destroy base class and recreate it in derived with this trick? class

  • 0

can i destroy base class and recreate it in derived with this trick?

class base: noncopyable
{
    base();         //ctor with none param
    base(int x);    //ctor with one param
    base(int x, int y); //ctor with two param

    virtual ~base();
}

struct params
{
    int x;
    int y;
    enum 
    {
        typeNoneParam,  //neither x nor y is defined
        typeOneParam,   //only x is defined
        typeTwoParam    //x and y both are defined
    }typeParam;
}

class Derived
{
    Derived(params p);  //construct base class conditionally by p.typeParam
}

Derived::Derived(params p)
    :base() //default typeNoneParam
{
    //typeNoneParam need not do special process

    if (p.typeParam == params::typeOneParam)
    {
        base::~base();  //delete the default-typeNoneParam creation by base-dtor
        base(p.x);      //recreate the new base with one-param base-ctor
    }
    if (p.typeParam == params::typeOneParam)
    {
        base::~base();  //delete the default-typeNoneParam creation by base-dtor
        base(p.x, p.y); //recreate the new base with two-param base-ctor
    }
}

All declaration for class derived and base cannnot change, struct params cannnot either.

Only Implementation of derived class is changing-permitted.

can anyone give idea about is that implematation right? And any other more gentle implementation satisfy this scenario(init noncopyable base class with dynamically-choosing base-ctor) well?

  • 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-09T04:00:09+00:00Added an answer on June 9, 2026 at 4:00 am

    The Derived class constructor depends on a valid Base class object being constructed first. By destroying the Base class I’m almost positive you’re flirting with Undefined Behavior. You might see this manifested with virtual functions for example.

    The proper way to do this is for the Derived class to pass the parameters into the Base class constructor as part of the initialization list:

    Derived(params p) : base(p) {};
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

class base { int a; protected: template<class T> class derived; public: base() {} virtual
can someone explain me why this script is not working? <script type=text/javascript> function destroy(ID)
I have class MyContainer < ActiveRecord::Base :has_many MyObjects, :dependent => :destroy end I want
I have a TakeAction model that looks like this: class TakeAction < ActiveRecord::Base belongs_to
I have 3 models. class Team < ActiveRecord::Base ... has_many :seasons_teams, :dependent => :destroy
Model I class TimeLog < ActiveRecord::Base has_one :custom_time_fields, :dependent => :destroy end Model II
I have following structure: class User < ActiveRecord::Base has_many :Hobbies, :dependent => :destroy accepts_nested_attributes_for
I have the following models: class Release < ActiveRecord::Base has_many :products, :dependent => :destroy
I have a Tag model: class Tag < ActiveRecord::Base has_many :taggings, :dependent => :destroy
class User < ActiveRecord::Base has_many :followings, :as => :followable, :dependent => :destroy, :class_name =>

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.