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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:35:45+00:00 2026-06-14T15:35:45+00:00

I override the base-class function ShowProperties in a sub-class, but still the base-class function

  • 0

I override the base-class function ShowProperties in a sub-class, but still the base-class function gets called. Why is this?

class hierarchy:

class CDiagramEntity : public CObject
{
public:
  virtual void ShowProperties( CWnd* parent, BOOL show = TRUE );
}

class CNetworkSymbol : public CDiagramEntity
{
  /*NO 'ShowProperties' Function*/
}

class CDeviceEntity : public CNetworkSymbol
{
  /*NO 'ShowProperties' Function*/
}

class CSwitch : public CDeviceEntity
{
public:
  virtual void ShowProperties( CWnd* parent, BOOL show = TRUE );
}

Use:

/*Use Here*/
{
  CDiagramEntity* obj = GetSelectedObject();
  if( obj )
  {
    CSwitch* sw = (CSwitch*)obj;
     sw->ShowProperties( this );    
     /*calls CDiagramEntity's function, not CSwitch's function*/
  }
}

PS:

  1. As @iammilind suggests, I removed virtual from CDiagramEntity and use casting, the function of CSwitch is called, and from the properties it shows(the properties can only be changed to what it shows when CSwitch is created), I am quite sure it is a CSwitch.
  2. As @user1610015 comments, if I use

    CSwitch* sw = dynamic_cast<CSwitch*>(obj);
    

    it returns NULL.

  3. As @Andrian Sham says, I got the problem’s reason:

    GetSelectedObject()--->finally calls--->
    {
      CDiagramEntity* result = NULL;
      if ( index < m_objs.GetSize() && index >= 0 )
        result = static_cast< CDiagramEntity* >( m_objs.GetAt( index ) );
      /*m_objs is defined as: CObArray m_objs;*/
      return result;
    }
    

m_objs:

CObArray m_objs;

And the object is stored as:

...( CDiagramEntity* obj )
{
  obj->SetParent( this );
  m_objs.Add( obj );
  SetModified( TRUE );
}

But can someone explain this in more details?

  • 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-14T15:35:46+00:00Added an answer on June 14, 2026 at 3:35 pm

    I am not sure how you do your debug. You said you can see a member var of CSwitch. If you mean you inspect CSwitch* sw = (CSwitch*)obj; and see such var in sw, you are in fact getting in wrong. Debugger is simply “interpreting” the piece of memory pointed by sw as it is a CSwitch. It doesn’t mean that it is really a CSwitch.

    I can only thought of a case. Have you put the object instance in any STL container and get it out to put it as the select item?

    (I forgot MFC totally, this is just an example)

    vector<CDiagramEntity> entities;  // note it is of type CDiagramEntity, not CDiagramEntity*
    
    CSwitch aCSwitch;
    entities.push_back(aCSwitch);
    
    // later
    SetItem("SomeValue", entities[i]);
    

    You may think you entities[i] should return an instance of CSwitch but it is not.

    (This is the best I can guess if you are pretty sure that the selected item should be a CSwitch)

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

Sidebar

Related Questions

I have a class like this: class someClass { public static function getBy($method,$value) {
I am a little confused by this concept. If I override a public function
I have an overridable sub in my base class Project1: Public Class BaseClass Protected
I have a base class like this class Base { public var space:Number; }
I have a base class for a BLL that includes the following function: public
i found this code: protected override void OnSourceInitialized(EventArgs e) { base.OnSourceInitialized(e); HwndSource hwndSource =
I am accessing data from sqlite data base in my following code. @Override public
Consider this code sample: public abstract class Parent { public int val; public Parent()
I have a base class that I want to look like this: class B
I have a base class like this: class base(object): name = def __init__(self,name): self.user

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.