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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:45:41+00:00 2026-05-24T11:45:41+00:00

class ClassB { int m_b; public: ClassB(int b) : m_b(b) {} void PrintClassB() const

  • 0
class ClassB {
    int m_b;
public:
    ClassB(int b) : m_b(b) {}
    void PrintClassB() const {
        cout << "m_b: " << m_b << endl;
    }
};


int main(int argc, char* argv[])
{
    const ClassB &af = ClassB(1);
    af.PrintClassB(); // print m_b: 1 with vs2008 & gcc 4.4.3
}

Given the above code, I have difficulties to understand this snippet:

Q1> What does this line mean?

const ClassB &af = ClassB(1);

Here is my understanding:

af refers to a temporary variable ClassB(1) and after the

execution of this line, the temporary variable is destroyed and af
refers to an undefined variable. During this procedure, no
copy-constructor is called.

Then why we can still issue the following statement and obtain the results?

af.PrintClassB(); // print m_b: 1 with vs2008 & gcc 4.4.3
  • 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-24T11:45:42+00:00Added an answer on May 24, 2026 at 11:45 am
    const ClassB &af = ClassB(1);
    

    const here extend the life time of the temporary object (i.e., ClassB(1)) being created. It’s scope lasts until af falls out of scope;

    af.PrintClassB(); // print m_b: 1 with vs2008 & gcc 4.4.3
    

    This is because, af is nothing but the temporary object’s reference which was constructed passing 1 to it’s constructor.

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

Sidebar

Related Questions

class A { public: void f() { cout << A() << endl; } };
public class GamePiece { public GamePiece(char cLetter, int nPointValue) { m_cLetter=cLetter; m_nPointValue=nPointValue; m_nTurnPlaced=0; //has
class a { protected: const int _ID; public: a::a(int id){}; a::top(int num); }; class
If I have the following class A { public: int stuff; void helper(B temp,
Consider the following class public class Class1 { public int A { get; set;
I have a Question class: class Question { public int QuestionNumber { get; set;
I have a post class : class Post { public int ID; public int?
Say I have this class: class myclass { public int Field1{ get; set; }
#include<iostream> using namespace std; class Abs { public: virtual void hi()=0; }; class B:public
class A : public QObject{ Q_OBJECT signals: void a_sig(); public: A(){ } public slots:

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.