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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:18:21+00:00 2026-05-10T21:18:21+00:00

I have run into a bit of a tricky problem in some C++ code,

  • 0

I have run into a bit of a tricky problem in some C++ code, which is most easily described using code. I have classes that are something like:

class MyVarBase { }  class MyVar : public MyVarBase {     int Foo(); }  class MyBase { public:      MyBase(MyVarBase* v) : m_var(v) {}     virtual MyVarBase* GetVar() { return m_var; } private:     MyVarBase* m_var; } 

I also have a subclass of MyBase that needs to have a member of type MyVar because it needs to call Foo. Moving the Foo function into MyVarBase is not an option. Does it make sense to do this:

class MyClass : public MyBase { public:     MyClass(MyVar* v) : MyBase(v), m_var(v) {}     MyVar* GetVar() { return m_var; } private:     MyVar* m_var; } 

This seems to work but looks really bad and I’m not sure if it’s going to cause a memory leak or break a copy constructor. My other options might be to name the MyVar variable in MyClass something else but have it be equal to the m_var pointer in the base, or to templatise MyBase on the MyVar type.

All these options don’t seem ideal so I wanted to know if anyone else has run into a situation like this and if there is a good way to make it work.

  • 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. 2026-05-10T21:18:22+00:00Added an answer on May 10, 2026 at 9:18 pm

    The correct way to do this is to have the variable only in the base class. As the derived class knows it must be of dynamic type MyVar, this is totally reasonable:

    class MyClass : public MyBase { public:     MyClass(MyVar* v) : MyBase(v) {}     MyVar* GetVar() { return static_cast<MyVar*>(MyBase::GetVar()); } } 

    Since MyVar is derived from MyVarBase, the different return-types of GetVar would still work if GetVar was virtual (as is the case here). Note that with that method, there must be no function in MyBase that can reset the pointer to something different, obviously.

    Note that static_cast is the right cast in that case. Using dynamic_cast, as proposed by one commenter, will tell the readers and users of GetVar that MyBase::GetVar() could return a pointer to an object not of type MyVar. But that doesn’t reflect our intention, as you only ever pass MyVar. To be consequent is the most important thing in software development. What you could do is to assert it is non-null. It will abort at runtime with an error-message in debug-builds of your project:

    MyVar* GetVar() {      assert(dynamic_cast<MyVar*>(MyBase::GetVar()) != 0);     return static_cast<MyVar*>(MyBase::GetVar());  } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey.. I have run into a bit of a problem with my python code..
I'm using SWFUpload to handle file uploads and have run into a problem where
I have run into a bit of a problem here: I had a problem-specific
I have run into a bit of a problem, well not sure if it
I'm learning Python, and have run into a bit of a problem. On my
I have run into a bit of a problem. Usually when dealing with UITableView
I have run into a bit of a snag in my code. I am
I have run in to a bit of a problem and I have done
I have run into a common, yet difficult problem. I do not use Photoshop
I have run into an issue with WPF and Commands that are bound to

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.