0

When we are passing object of a class to function as value the copy constructor called. it will be in continuous loop. but how its working in case of string.

For example:

#include <iostream>
#include <string>

using namespace std;

string read_string(std::string s)
{
    std::string test;
    cout<<s;
    test=s;
    return test;
}

int main() 
{
    string sir = "start";
    cout << "SIR starts out as : '" << sir << "'" << endl;
    sir = read_string(sir);
    cout << "and becomes '" << sir << "', after return from function." << endl << endl;
    return 0;
}

Here read_string(sir), we are passing sir a string object, and in the function definition we are handling as value.

Please clear the doubts.

iBug
  • 35,554
  • 7
  • 89
  • 134
sss
  • 21
  • 3
  • 2
    `std::string` is a class like any other. What exactly do you find confusing? – Quentin Feb 23 '18 at 12:00
  • 2
    What do you mean by "in continuous loop"? – iBug Feb 23 '18 at 12:00
  • @Quentin, std::string is class how can we pass object of this class to the function – sss Feb 23 '18 at 12:02
  • 1
    One could write their own class, with output in various constructors, and destructor, to see what happens to it, at various stages at execution. – Algirdas Preidžius Feb 23 '18 at 12:03
  • @iBug, if we pass object by value in case copy constructor its internally call again the copy constructor , So it will be in loop – sss Feb 23 '18 at 12:04
  • @sss that's what you're doing. The `s` parameter is copy-constructed from `sir` when entering the function, as you mentioned. – Quentin Feb 23 '18 at 12:04
  • Indeed, the copy constructor cannot take its parameter as a value, otherwise you'd have infinite recursion. But all other functions can then use the copy constructor to copy their arguments. – Quentin Feb 23 '18 at 12:05
  • @sss 1) "_how can we pass object of this class to the function_" In the manner that you just did. It is, still, unclear, what is it that you find confusing. 2) Why do you think that copy-constructor would need to call copy-constructor again? 3) Consider learning C++ in a structured way, from a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Algirdas Preidžius Feb 23 '18 at 12:06
  • @Quentin, Yes, then it will be in loop only ryt..pass by value – sss Feb 23 '18 at 12:06
  • In case of nested calls to the same function, please talk about *recursion* and if the recursion doesn't stop before getting a StackOverflow, talk about (probably) *endless recursion* or something similar. The term *loop* is used differently in programming and it is very confusing to have it in this question.. – grek40 Feb 23 '18 at 12:18

2 Answers2

4

The copy constructor does not take the original object by value, otherwise it would have caused an infinite recursion, as you've said. In fact, all copy constructors take the original object by reference. Their signature is

T::T(const T&);

In this way, the copy constructor can access the original object (instead of a copy) as a const reference, so it can perform necessary "copying" actions.

iBug
  • 35,554
  • 7
  • 89
  • 134
0

The basic difference between pass by value and pass by reference is :

  • pass by reference deals with the original data (same memory ) which is passed to the function,

  • pass by value copies the data to a different (new and temporary) memory location which is in scope of the function ( I.e. created and destroyed within that function).

This rule applies to the basic types (such as int, float, double) as well as to custom types (such as class objects, std::string etc. )

Sitesh
  • 1,816
  • 1
  • 18
  • 25