0

I am trying to learn by experimenting with constructors and references. I wrote the class as follows and I expect the answer written after the class

#include <iostream>
#include <string>

class human {
 public:
  const std::string& m_name;
  void printit() { 
    std::cout <<"printit "<< m_name << std::endl;
  }
  human(const std::string& x):m_name(x){
    std::cout << "the constructor "<< m_name << std::endl;
  }
  ~human() {
    std::cout << "dest called"<< std::endl;
  }

};

int main()
{
  human x("BOB")
  x.printit();
  return (0);
}

>     the constructor BOB
>     printit BOB
>     dest called

However I get something like this. m_name is lost when I call the function. The same class works fine when using int in place of string. What is the problem?

>     the constructor BOB
>     printit
>     dest called

Now the same class with int const reference

#include <iostream>

class human {
 public:
  const int& m_name;
  void printit() { 
    std::cout <<"printit "<< m_name << std::endl;
  }
  human(const int& x):m_name(x){
    std::cout << "the constructor "<< m_name << std::endl;
  }
  ~human() {
    std::cout << "dest called"<< std::endl;
  }

};

int main()
{
  human x(3);
  x.printit();
  return (0);
}

I get the correct answer in this case.

>     the constructor 3
>     printit 3
>     dest called

Can someone explain the reason? Do builtin types persist?

Praetorian
  • 106,671
  • 19
  • 240
  • 328
Arul Moondra
  • 131
  • 1
  • 9
  • Can someone explain why it works for integers. – Arul Moondra Mar 19 '16 at 00:13
  • If you're replacing the `const string&` data member with `const int&`, then it's still undefined behavior. So it doesn't really *work*, but undefined behavior can make it seem like it does sometimes. – Praetorian Mar 19 '16 at 00:17
  • References are used only when you want a function or method to **change** a string or array or structure, and allow the changed data-item be available to the calling function. – Arif Burhan Mar 19 '16 at 00:22
  • I was learning by experimenting as I said and wanted to see what happens. Learned something new. – Arul Moondra Mar 19 '16 at 00:27
  • @Praetorian I am referring to th approach in the link and printing the values. Should I consider that int should also fail and I am just getting lucky here http://stackoverflow.com/questions/14169584/passing-and-storing-a-const-reference-via-a-constructor – Arul Moondra Mar 19 '16 at 00:32
  • @Arul Yes. See the [first comment](http://stackoverflow.com/questions/14169584/passing-and-storing-a-const-reference-via-a-constructor#comment19633090_14169598) under the answer you linked to. – Praetorian Mar 19 '16 at 00:42
  • @Arul See what happens when you create two objects in your `int` example - http://coliru.stacked-crooked.com/a/129095e6cd9554b5 – Praetorian Mar 19 '16 at 00:50
  • @Praetorian Well on visual studio 15 it works perfectly. – Arul Moondra Mar 19 '16 at 00:55
  • @Arul Well there's a reason it's called undefined behavior. – Praetorian Mar 19 '16 at 00:58

0 Answers0