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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:26:54+00:00 2026-06-15T21:26:54+00:00

Observe the following code: class A { public: virtual void Foo() = 0; int

  • 0

Observe the following code:

class A { public: virtual void Foo() = 0; int Bar; };

class B : public A { public: B( float X ); void Foo(); private: float X; };

class C : public A { public: C( float Y ); void Foo(); private: float Y; };

Now, in some function let’s say I do this:

A*& pToA = pToDerived; // allocated as A* pToDerived = new B( ... );

pToA     = pToC; // allocated as A* pToC = new C( ... );

Is this valid? If so, will it cause a memory leak even though pToA is a reference to a pointer of pToDerived?

SSCS

Assume that Node has, say, 2 or more types which derive from it. This is a snippet from a linked list I’m implementing at the moment, which will likely require polymorphism in order to work properly, as it’s not meant to be generic. newnode is the argument passed to an insert function.

    Node* iNode;

    for( iNode = mStart; iNode != mEnd; iNode = iNode->Next )
    {
        if ( iNode->Key == k ) // Replace current node with newnode
        {
            newnode->Next = iNode->Next;
            newnode->Prev = iNode->Prev;

            *iNode        = *newnode;

            delete newnode;

            return;         // We're done, so we quit.
        }
    }   

    // Node doesn't alreay exist, so we add it.

    Node*& uglyhack = mEnd;

    iNode->Next   = newnode;
    newnode->Prev = iNode;
    uglyhack      = newnode;
  • 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-15T21:26:55+00:00Added an answer on June 15, 2026 at 9:26 pm

    Let’s say you have the following:

    // See http://codepad.org/8mG6YiLy
    class A { public: virtual void Foo() = 0; int Bar; };
    
    // Added definitions for these so that this would compile
    class B : public A {
        public: B( float x ) : X(x) {}
        virtual void Foo() {} // Once virtual, always virtual
        private: float X;
    };
    
    class C : public A {
        public: C( float y ) : Y(y) {}
        virtual void Foo() {}
        private: float Y;
    };
    
    int main()
    {
        A* pToDerived = new B(3.14);
        // pToDerived -> B
    
        A*& pToA = pToDerived;
        // pToDerived -> B
        // pToA IS pToDerived
    
        A* pToC = new C(2.718);
        // pToDerived -> B
        // pToC -> C
        // pToA IS pToDerived
    
        pToA = pToC;
        // pToDerived -> C
        // pToC -> C
        // pToA IS pToDerived
        // One object of type B no longer has pointers pointing to it
    }
    

    This code leaks two objects — one of type B (which happens at the pToA = pToC statement) and one of type C (which happens when main returns). The “reference” in this case doesn’t actually enter into much of play here. In C++, references are just aliases for some other object. You cannot “reseat” them. That is, once a reference references something, it can never reference anything else ever again.

    In this case, when you created pToA, you created a reference to the pointer — the pointer in this case being pToDerived. Making this reference has no effect on memory managment or responsability of some code to call delete in the right place whatsoever.

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

Sidebar

Related Questions

Why does this happen? Please observe the following code: static class StringExtension { public
Consider the following code: public class ReadingTest { public void readAndPrint(String usingEncoding) throws Exception
I wrote the following code snippet: void foo() { struct _bar_ { int a;
Observe the following .NET type: public class X { public DateTime Timestamp {get;set;} public
Given the following class based on containers of shared pointers, class Foo; class Bar
I have the following code in a file called overlay.js: Event.observe(window, 'load', function() {
I observe that my tan(float) function from the cmath library is returning a negative
Observe the following simple source code: using System; using System.Linq.Expressions; using System.Reflection; using System.Reflection.Emit;
I have the following code on WCF Service and I am using basicHttpBinding public
Let's observe the following segment of code in Java that uses System.currentTimeMillis() in 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.