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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:54:06+00:00 2026-05-20T17:54:06+00:00

I have three classes: Base, Derived (inherits from Base), and Stats (which uses a

  • 0

I have three classes: Base, Derived (inherits from Base), and Stats (which uses a Base).

The program makes a Derived object, which may be deleted and reconstructed several times during the program’s execution. It also sets up a Stats object which will only be created once, but needs to call functions on the Base of the Derived object. Because the Derived object may be reconstructed, the Stats object will need a reference to a pointer of Base, since the value of the pointer may change. However, when I construct a new Derived in main, the reference in the Stats class doesn’t see the new object.

In the example below, d and m_obj both are null, then when I make a new Derived instance, m_obj still is null. This doesn’t make sense to me. What confuses even more, is if I change the line Derived* d = 0; to Base* d = 0;, it works fine. Any thoughts?

#include <iostream>
using namespace std;

class Base {
};

class Derived : public Base {
};

typedef Base* const base_ptr;

class Stats {
public:
    Stats(Base * const &obj) : m_obj(obj) {
        cout << "In Stats():" << endl;
        cout << "   m_obj = " << m_obj << endl;
    }

    void f() const {
        cout << "In f:" << endl;
        cout << "   m_obj = " << m_obj << endl;
    }

private:
    base_ptr &m_obj;
};

int main() {
    Derived* d = 0;
    cout << "d = " << d << endl;

    Stats s(d);

    d = new Derived();
    cout << "d (after new) = " << d << endl;

    s.f();

    return 0;
}
  • 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-20T17:54:06+00:00Added an answer on May 20, 2026 at 5:54 pm

    You are creating a reference to a temporary Base * that points to what d was pointing at (NULL in this case). That temporary is destroyed after line that invokes the constructor finishes executing. Congratulations, you’ve just invoked undefined behavior! After that, anything can happen.

    Essentially, this line reads like this if you expand out what’s really happening:

    Stats s(d);
    

    really:

    {
        Base * const tmp = d;
        Stats s(tmp);
    }
    

    Except, of course, s doesn’t go away like it would in this example.

    When d is of type Base * then the compiler doesn’t have to do any type conversion and so doesn’t have to create any temporaries.

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

Sidebar

Related Questions

Let's say I have a Base class and several Derived classes. Is there any
Suppose I have three classes. It is valid to instantiate A, but there are
Say I have three classes: class X{}; class Y{}; class Both : public X,
If I have three classes, A, B, C. A and B are friends (bidirectionally).
I have three (C++) classes: Player, Hand, and Card. Player has a member, hand,
I have a three nested classes, Show, Season and Episode, where a show has
I have a basic class that derived subclasses inherit from, it carries the basic
I have created a debugger visualizer in VS2008. There are two classes i've made,
I have the following 3 classes Book Product SpecialOptions There are many Books, and
is there any way to make IE6 understand double classes, say I have a

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.