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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:14:51+00:00 2026-05-27T22:14:51+00:00

// have compilation errors class TestClass { public: TestClass(std::string& str) {} }; int main()

  • 0
// have compilation errors
class TestClass
{
public:
  TestClass(std::string& str) {}

};

int main() {
  TestClass tc(std::string("h"));
  return 0;
}


p166.cpp: In function ‘int main()’:
p166.cpp:29:32: error: no matching function for call to ‘TestClass::TestClass(std::string)’
p166.cpp:25:3: note: candidates are: TestClass::TestClass(std::string&)
p166.cpp:23:1: note:                 TestClass::TestClass(const TestClass&)


// have NO compilation errors
class TestClass2
{
public:
  TestClass2(const std::string& str) {}

};

int main() {
  TestClass2 tc(std::string("h"));
  return 0;
}

Question> Why the first part (TestClass) has compilation errors?
Is it because that a NON-const reference cannot refer to a temporary object while a const-reference can refer to a temporary object?

Thank you

  • 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-27T22:14:51+00:00Added an answer on May 27, 2026 at 10:14 pm

    Is it because that a NON-const reference cannot refer to a temporary object while a const-reference can refer to a temporary object?

    Yes, as simple as that. Temporary objects may bind to const references but not to non-const ones.

    Note that in C++11, we get rvalue references (T&&), which will only bind to rvalues (temporaries amongst other things). See this question for more info on lvalues, rvalues, and all those other things.

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

Sidebar

Related Questions

I have a private const int defined in a C# class. I want compilation
I have the following (cut-down) class definition, and it has compilation errors. #include <iostream>
I have a class that contains two methods like these: public String getFoo(Int32 a)
You get a compilation error if you define the string parameter to have a
We have a singleton template class as defines below template<class T> class Singleton{ T&
Imagine I have this class namespace CommonLibrary { public class Report() { public DateTime
I have this script import unittest,itertools,random ##testclass class Testcomb(unittest.TestCase): def test_input(self): self.assertRaises(TypeError,calculate_combinations,dict(comb1), 5) def
I have a post-compilation step that manipulates the Java bytecode of generated classes. I'd
We have 2 different compilation machine: red hat as4 and as5. Our architects require
I have a C/C++ source file with conditional compilation. Before I ship it to

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.