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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:02:58+00:00 2026-05-23T12:02:58+00:00

Lemme explain my problem by giving an example: #include <iostream> class PC { public:

  • 0

Lemme explain my problem by giving an example:

#include <iostream>

class PC
{
public:
    PC():Data(0)
    {
    }
    virtual void display()
    {
        std::cout<<"The data is :"<<Data<<std::endl;
    }
protected:
    int Data;
};

class SmartPC:private PC
{
public:
    SmartPC():PC()
    {
    }
    void convert()
    {
        PC* temp=static_cast<PC*>(this);
        temp->display();
    }
    void display()
    {
        std::cout<<"The data is (in bb):"<<a<<std::endl;
    }
};

int main()
{
    SmartPC SmrtPC;
    PC* miniPC= static_cast<PC*>(&SmrtPC);
    SmrtPC.convert();
}

According to Scott Meyers : static_cast<PC*>(this); will create a temp base copy of SmartPC. But temp->display(); executed the display() function of derived class. Why is that so? Shouldn’t it execute the function of base’s display(), since the object is now completely a copy of base of SmartPC?

Another question is that if I add the line temp->data; in convert() function , it says
PC::Data is protected but I am accessing it from derived class scope i.e SmartPC, so why doesn’t it work?

Any help is appreciated.

  • 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-23T12:02:58+00:00Added an answer on May 23, 2026 at 12:02 pm

    According to scott meyers : static_cast<PC*>(this); will create temp base copy of SmartPC. but temp->display(); executed the display() function of derived class why is it so? it should execute function of base’s display() , since the object is now completely a copy of base of SmartPC.

    No copy is created, you are only casting a pointer.

    Since the class is polymorphic, calling a virtual function via a pointer results in calling the “correct” version of the function (according to the dynamic type of the object, which is SmartPC *).

    If, instead, display was not virtual, the version of the base class would have been called, since for nonvirtual methods it’s the static type of the pointer to determine which version is to be called.

    (display is virtual also in SmartPC even if it’s not explicitly specified, the virtual qualifier is implied when overriding inherited virtual functions)


    Notice instead that if you did:

    PC temp(*this);
    

    you would have actually created a copy of the current object, “sliced” to be an object of type PC. This is called “object slicing” and is performed by the copy constructor of PC; most often this is undesired behavior (because what was an object of the derived class actually becomes an object of the base class, and polymorphism do not work as some may expect).


    Another question is that if i add the line temp->data; in convert() function , it says PC::Data is protected but i am accessing it from derived class scope i.e SmartPC, so why doesn’t it works?

    Conceptually, when you try to access temp->data you’re trying to access a private member of another object (it’s not important that temp is actually this), so the access is denied. Your “derived class privileges” to access protected members work only on this.

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

Sidebar

Related Questions

I have a problem of listing duplicated rows that include NULL columns. Lemme show
i was blocked by a coldfusion problem, any suggestions are appreciated. now lemme decribe
I wasn't sure how to write topic correctly but lemme describe what problem I
Ok, lemme start out by saying Im new to this! LOL I have done
The problem: whenever, if i have the single '=' the variable will show up
I've a problem getting the tab ID of a tab and using it in
I have a problem with the inheritance of Doctrine within my fixtures.yml file of
I'm trying to create a subset of a table (as a materialized view), defined
Does GWT has LazyPanel .I can not see it .Please let me know .If
I have come across a bizarre error in the Magento shop I'm developing and

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.