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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:18:45+00:00 2026-05-15T20:18:45+00:00

I asked a question an hour or two ago that is similar but this

  • 0

I asked a question an hour or two ago that is similar but this is fundamentally different to me. After this I should be good.

class base
{
private:
    string tame;
public:
    void kaz(){}
    virtual ~base() {}
    void print() const
    {
        cout << tame << endl;
    }
};

class derived: public base
{
private:
    string taok;
public:
    std::string name_;
    explicit derived( const std::string& n ) : name_( n ) {}
    derived(){}
    void blah(){taok = "ok";}
    void print() const
    {
        std::cout << "derived: " << name_ << std::endl;
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    base b;
    derived d;

    base * c = &b;
    derived * e = (derived *)&b;

    e->kaz();
    system("pause");


    return 0;
} 

I know downcasting in in this example is not good practice but I’m just using it as an example. So when I now am pointing to a base object from a derived pointer, I don’t get why I am still able to do certain operations only belonging to the base class.

For example, the base class’s interface has a Kaz() method but the derived method does not. When I downcast, why does the compiler not yell at me for doing this even though Kaz() is not part of the derived class’s interface?

  1. Why is the compiler not complaining for using members of the base class when I am using a derived pointer?

  2. Why does the compiler yell at me only when I access a member from the base class interface from within a method but not directly?

For example:

I can’t do this:

e->print() //Program crashes

But I can do this:

e->tame = "Blah";
cout << e->tame << endl;
  • 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-15T20:18:46+00:00Added an answer on May 15, 2026 at 8:18 pm

    The derived class inherits all the members of the base class, so kaz() exists also for derived objects. If you call kaz() on a derived object, simply the method that was inherited from base is called. If you access the inherited members from within a method or directly doesn’t matter.

    The problem with e is that it is really pointing to a base object, not a derived. With the cast e = (derived *)&b you tell the compiler “I know it doesn’t look like it, but this really is a derived *, believe me!”. And the compiler believes you, since you are the master. But you lied and &b was actually not a derived*. Therefore horrible things happen when the compiler tries to call derived::print() on it, in this case it leads to a crash of the program.

    When you access e->tame directly, also horrible things could happen (the compiler still treats e as a derived* while it only is a base*). In this case, by chance, it happens to print out the expected value anyway.

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

Sidebar

Related Questions

I have just asked this question an hour ago but with regards to IE8
Note: I originally asked this question about an hour ago but only recently realized
I'm sure this question has been asked a thousand times but after an hour
This is an extension for this question asked an hour ago. We cannot modify
I asked this question 1 hour ago: How can I transfer my data from
I've already asked question similar to this some time ago, I thought I solved
This question has been asked before but I am facing a slightly different problem.
I know this question was asked a lot, but I didn`t find anything that
I've just asked a question about an hour ago, while waiting for replies, I've
I asked this question over Security site, and people there suggested I should 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.