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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:10:59+00:00 2026-05-27T14:10:59+00:00

Given an object initialized like so: Base* a = new Derived(); Container<Base> c(a); where

  • 0

Given an object initialized like so:

Base* a = new Derived();
Container<Base> c(a);

where

class Base {
  ...
  protected:
    ~Base();
}

class Derived : public Base {...};

template <typename T>
class Container {
  private:
    T* object;

  public:
    Container(T* o) : object(o) {}
    void deleteObject() {
      delete object;  // Object must be casted to (unknown) derived type to call destructor.
    }
};

Obviously this is very simplified from the actual code, but the question is how do I cast object from its templated type to its actual, derived type (if they are different), which is not known?

I cannot modify Base or Derived, or even any of the code calling Container, only the Container class itself.

  • 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-27T14:11:00+00:00Added an answer on May 27, 2026 at 2:11 pm

    If you’re able to change the creation code, you might get away with this:

    template<class T>
    void deleter(void* p){
      delete static_cast<T*>(p);
    }
    
    template<class T>
    class Container{
    private:
      T* obj;
      typedef void (*deleter_func)(void*);
      deleter_func obj_deleter;
    
    public:
      Container(T* o, deleter_func df)
        : obj(o), obj_deleter(df) {}
      void deleteObject(){ obj_deleter(obj); }
    };
    

    And in the calling code:

    Base* a = new Derived();
    Container<Base> c(a, &deleter<Derived>);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given: Object innerProxy = ... Object proxy = java.lang.reflect.Proxy. newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[]{type}, innerProxy); How
Given an object like: class M(models.Model): test = models.BooleanField() created_date = models.DateTimeField(auto_now_add=True) Given sample
Given this object class Contact { public IEnumerable<Person> People { get; } public string
Is there any way I can construct an new object from the given object
I would like to give a class a unique ID every time a new
I have loaded image into a new, initialized Oracle ORDImage object and am processing
Given this powershell code: $drivers = New-Object 'System.Collections.Generic.Dictionary[String,String]' $drivers.Add(nitrous,vx) $drivers.Add(directx,vd) $drivers.Add(openGL,vo) Is it possible
Every object I know of in Python can take care of its base class
class Flarg { private readonly Action speak; public Action Speak { get { return
Given a custom, new-style python class instance, what is a good way to hash

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.