C ++ constructor - passing by reference only works with a constant. What for?

So, earlier today I was catching up with the good old C ++, and when I compiled my code, it did not work. Like some programmers, I started to crack, and eventually found that adding a keyboard constfixes the problem. However, I don’t like to hack a lot and want to find out why the code worked fine after adding const.

This was my code BEFORE adding constto the constructor:

#include <iostream>
#include <string>

using namespace std;

class Names {
private:
    string _name;
    string _surname;

public:
    Names(string &name, string &surname) : _name(name), _surname(surname) {}

    string getName() const {return _name;}
    string getSurname() const {return _surname;}
};

int main(){
    Names names("Mike", "Man");

    cout << names.getName() << endl;
    cout << names.getSurname() << endl;
}

I was getting these errors:

names.cc:19:27: error: no matching function for call to β€˜Names::Names(const char [5], const char [4])’
  Names names("Mike", "Man");
                           ^
names.cc:19:27: note: candidates are:
names.cc:11:2: note: Names::Names(std::string&, std::string&)
  Names(string &name, string &surname) : _name(name), _surname(surname) {}
  ^
names.cc:11:2: note:   no known conversion for argument 1 from β€˜const char [5]’ to β€˜std::string& {aka std::basic_string<char>&}’
names.cc:5:7: note: Names::Names(const Names&)
 class Names {
       ^
names.cc:5:7: note:   candidate expects 1 argument, 2 provided
<builtin>: recipe for target 'names' failed
make: *** [names] Error 1

However, after adding a keyword constinside the constructor Names(string const &name, string const &surname) : _name(name), _surname(surname) {}, it works.

This is my working code:

#include <iostream>
#include <string>
using namespace std;

class Names {
private:
    string _name;
    string _surname;

public:
    Names(string const &name, string const &surname) : _name(name), _surname(surname) {}

    string getName() const {return _name;}
    string getSurname() const {return _surname;}

};

int main(){
    Names names("Mike", "Man");
    cout << names.getName() << endl;
    cout << names.getSurname() << endl;
}

Now a few questions:

  • const ? , , const ?
  • , , : Names(string name, string surname) : _name(name), _surname(surname) {} , _name _surname   . pass by value,   .   ? .

+4
3

A std::string, , .

+13

- @CoryKramer

std::string, , .

.

, , (@CodeMan)

#include <iostream>

std::ostream& logger = std::clog;

struct A {
    A() = delete;
    A(int ii) : i(ii) {logger << "A(int) ctor\n";}
    A(const A& a) : i(a.i) {logger << "A copy ctor\n";}
    A& operator= (const A& a) { i = a.i; logger << "A copy assigment\n"; return *this;};
    // nothing to steal here
    A(A&& a) : i(a.i) {logger << "A move ctor\n";}
    A& operator= (A&& a) {i = a.i; logger << "A move assigment\n"; return *this;};
    ~A() {logger << "A dtor\n";}

    int i;
};

void foobar(const A& a) {
    logger << "foobaring const A&\n";
}

void foobar(A& a) {
    logger << "foobaring A&\n";
}


int main(){
  int i(42);  
  A a(i);
  logger << "ctored a\n===================\n";  
  foobar(a);
  logger << "foobared a\n-------------------\n";  
  foobar(i);
  logger << "foobared " << i << "\n===================" << std::endl;
}

live Coliru's.

[...]
===================
foobaring A&
foobared a
-------------------
A(int) ctor
foobaring const A&
A dtor
foobared 42
===================
[...]

foobar A ctored int A , const foobar, , const A &.

, foobar A .

+2

1- "" "" , . const.

2- If you go through the value _nameand _surname, we get the transmitted values. The passed copies of the parameters are local to the function, so they are destroyed when the constructor goes out of scope.

0
source

Source: https://habr.com/ru/post/1615046/


All Articles