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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:37:36+00:00 2026-06-09T00:37:36+00:00

Consider the following code: #include <iostream> using namespace std; class B{ public: B(){} };

  • 0

Consider the following code:

#include <iostream>
using namespace std;
class B{
public:
  B(){}
};
class A
{
public:
A(){}
A(B &b){

}
A(const B &b){
       cout<<"cccddd"<<endl;
}
};
int main()
{
   B b;
   A c(b);
   A a;
   a=b; //ok
   A &ref = b; //error and why???
}

why b assigned to a is ok,but b assigned to ref is illegal???

  • 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-09T00:37:38+00:00Added an answer on June 9, 2026 at 12:37 am
    A(const B &b){
           cout<<"cccddd"<<endl;
    }
    

    This will enable you to convert an object of type B into an object of type A. However, a reference T& cannot be created from another type L.

    8.5.3 References [dcl.init.ref]

    1. A variable declared to be a T& or T&&, that is, “reference to type T” (8.3.2), shall be initialized by an object, or function, of type T or by an object that can be converted into a T.

    2. […]

    3. […]

    4. Given types “cv1 T1” and “cv2 T2,” “cv1 T1” is reference-related to “cv2 T2” if T1 is the same type as T2, or T1 is a base class of T2. “cv1 T1” is reference-compatible with “cv2 T2” if T1 is reference-related to T2 and cv1 is the same cv-qualification as, or greater cv-qualification than, cv2 […].

    In your example neither are A and B the same type, nor is A a base class of B, thus both classes aren’t reference-related.

    Why won’t A &ref = (A)b work?

    The expression (A) b will actually create a temporary, which cannot be bound to a normal reference:

    error: invalid initialization of non-const reference of type ‘A&’ from a temporary of type ‘B’

    You would need to a const reference A const & ref = (A)b for this.

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

Sidebar

Related Questions

Consider the following sample code: #include <iostream> using namespace std; class base { public:
Consider the following sample code. #include <iostream> using namespace std; class base { public:
Consider the following code: #include <iostream> using namespace std; class Test { static int
Consider this code #include <iostream> #include <cstdio> using namespace std; class Dummy { public:
Consider the following code: #include<iostream> using namespace std; class sample { int a; int
Consider the following sample code below: #include <iostream> using namespace std; class base {
please consider following code #include <iostream> using namespace std; class Digit { private: int
Consider the next code : #include <iostream> using namespace std; class A { public:
Consider the following code: #include <iostream> #include <cstdio> #include <cstring> using namespace std; template<class
Consider the following code: #include <iostream> using namespace std; int main() { int x,

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.