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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:01:33+00:00 2026-06-13T02:01:33+00:00

Take two classes that both can only be created by new . One class

  • 0

Take two classes that both can only be created by new. One class is the base and the other is a derivative. The derived class only adds methods.

class Base
{};

class Derived : public Base
{};

Base * b = new Base{}
Derived * d = covert( b );

// - or -

Base * b = new Base{};
convert( b ); // converts Base to Derived
Derived * d = dynamic_cast<Derived *>(b);

What I want to do is take the Base class data that has been allocated and extend/wrap with the derivative via some method or function, convert.

Update:
Building for embedded systems memory is scarce, so I am doing everything I can to reduce that amount of memory allocation. I was just wondering if there was a way to just sort of extend the base class already allocated memory and wrap it with the derivative.

More Updates:
Although the embedded system is ARM and I am currently using LLVM compiler this might not be true in the future. So a standard compliant way is preferred.

  • 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-13T02:01:34+00:00Added an answer on June 13, 2026 at 2:01 am

    If I understand correctly your question one possible solution is to use aggregation instead of inheritance.

    class Base
    {/*has only the data*/}
    
    class Derived 
    {
        Base &base;
        Derived(Base &b) : base(b) {}
        //now methods from derived will use the data members from instance passed in constructor
        //if is possible the Derived needs to be a friend class of Base in case there are no getter for all members
    }
    

    Instead of reference we can use a smart pointer if necessary.
    This way you avoid the cast by constructing a new Derived object which uses the data from the a Base object.

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

Sidebar

Related Questions

I have two classes, Client and Author , that are both derived from Member
I want to take two lists and find the values that appear in both.
take two following classes: class Test1{ public: Test1()=default; Test1(char in1,char in2):char1(in1),char2(in2){} char char1; char
Take these two Java classes: class User { final Inventory inventory; User (Inventory inv)
Take these two classes for example (C++) class B1 { public: }; class B2
I need to have two classes, one class has two methods each of which
I'm using the Unity dependency injection framework. I have two classes, that each take
I got the following problem: There is an abstract base class, and two other
I want to create a class that can take different types of value in
Take these two classes for instance: struct Owned { Owned() : i() { }

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.