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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:32:06+00:00 2026-05-14T06:32:06+00:00

I have a c++ class derived from a base class in a framework. The

  • 0

I have a c++ class derived from a base class in a framework.

The derived class doesn’t have any data members because I need it to be freely convertible into a base class and back – the framework is responsible for loading and saving the objects and I can’t change it. My derived class just has functions for accessing the data.

But there are a couple of places where I need to store some temporary local variables to speed up access to data in the base class.

mydata* MyClass::getData() {
   if ( !m_mydata ) { // set to NULL in the constructor
      m_mydata = some_long_and complex_operation_to_get_the_data_in_the_base()
   }  
   return m_mydata;
}

The problem is if I just access the object by casting the base class pointer returned from the framework to MyClass* the ctor for MyClass is never called and m_mydata is junk.
Is there a way of only initializing the m_mydata pointer once?

  • 1 1 Answer
  • 1 View
  • 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-14T06:32:06+00:00Added an answer on May 14, 2026 at 6:32 am

    It doesn’t have members and you must maintain bit-for-bit memory layout compatibility… except it does and C++ doesn’t have a concept of freely-convertible.

    If the existing framework allocates the base objects, you really can’t derive from it. In that case, I can think of two options:

    • Define your own class Cached which links to Base by reference. Make the reference public and/or duplicate Base‘s interface without inheritance.
    • Use a hash table, unordered_map< Base *, mydata > mydata_cache;. This seems most appropriate to me. Use free functions to look up cache data before delegating to the Base *.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class which is derived from a base class, and have many
I have a class Derived that inherits directly from two base classes, Base1 and
I have a class derived from Dictionary. I need this class to simulate a
I have created a derived class from a base class and have added the
I have a single class Base, and a few tens of classes derived from
I have a class derived from base class which has a private field. How
I have a class Derived<T> that is derived from Base<T> . Which of the
I have a class which derived from wx.frame and need to attach it to
I have two classes - one base class and one derived from it :
Possible Duplicate: Calling virtual function of derived class from base class constructor? I have

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.