So earlier on today, I was catching up with good old C++ and when I was compiling my code it was not working. Like a few programmers, I started hacking about, and eventually found that adding the keyboard const
cured the problem. However, I don't like hacking about a lot and want to find out why the code was working fine after adding the const
.
This was my code BEFORE adding the const
to 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 the const
keyword within the constructor Names(string const &name, string const &surname) : _name(name), _surname(surname) {}
-- it seems to be working.
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:
- Why was the code not working without the
const
for pass by reference? Is it good practice to always pass by reference in your constructors, and if so does it mean we have to use theconst
keyword? - So if I passed by value in the constructor say:
Names(string name, string surname) : _name(name), _surname(surname) {}
does this mean that_name
and_surname
are null or are they the values passed. I know in pass by value, a copy of the variable is being made and changes are being made to the copy. But when does the copy get destoryed or out of scope? It is a bit confusing.
Thanks