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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:09:53+00:00 2026-06-04T16:09:53+00:00

I have the following class: class A { public: B& getB() {return b;} private:

  • 0

I have the following class:

class A
{
public:
   B& getB() {return b;}    
private:   
    B b;
};

class B
{
   ~B() {cout<<"destructor B is called";}
...

};

void func()
{
   A *a = new a;
   B b = a->getB();
   .....
}

Why does the destructor of class B is called when exiting the function func? Doest the function getB return a referance to the object B? if class A still exists at the end of function func, why does the destructor of B is called?

  • 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-04T16:09:55+00:00Added an answer on June 4, 2026 at 4:09 pm

    When you have:

    B b = a->getB();
    

    a new object of type B is created from a reference to existing instance of B (B&). It is not the B::operator= that is called here but copy constructor.

    Each class has a copy constructor (if you don’t add it explicitly, compiler will provide one for you). It accepts a single argument which is a reference to the same class. You haven’t put copy constructor in the code above so I assume that compiler has generated one for you:

    class B
    {
    public:
       B(B& other)
       {
          // memberwise copy (shallow copy) 
       };
    };
    

    So A::getB() returned a reference to member A::b and this reference was passed as an argument to B::B(B&).

    void func()
    {
       A *a = new A();  // Instance of A is created on the heap;
                        // (pointer a is a local variable and is on the stack though!)
                        // A::b is object of type B and it is on the heap as well  
    
       B b = a->getB(); // Instance of class B is created on the stack (local variable)
       .....
       delete a;        // deleting A from the heap: 
                        // A::~A is called which calls B::~B (of its member b)
    } // a and b go out of the scope; b is an object => B::~B is called                    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following class public class ButtonChange { private int _buttonState; public void SetButtonState(int
I have the following class: Class L{ public: bool foo(vector<bool> & data); private: C**
I have the following class: class BritneySpears { public: int getValue() { return m_value;
If i have the following: class Array{ public: Array (int n=2) : _n(n), buf(new
I have the following C++ class: class Eamorr { public: redispp::Connection conn; Eamorr(string& home,
I have the following class class CItem { public: CItem(CRegistry &Registry) _Registry(Registry) {Registry.Register();} ~CItem()
I have the following code: class Array { public: int aaa; Array():aaa(1){} void print()
I have the following class: public class MainActivity extends Activity { /** Called when
I have the following class: class StringHolder { public: StringHolder(std::string& str) { m_str =
Suppose I have the following: class dataClass { public: int someData; float moreData; void

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.