0

I have started learning c++ recently. So, here is a quick doubt that I have:

Learning Part I:
While I was reading about list initialization in c++, I got an idea about the difference between direct initialization and copy initialization.

string name = "Deb"; //Copy
string name("Deb"); //Direct

So, I thought - Okay, it's a good thing to stick with direct initialization as the c++ primer suggests copy initialization is somewhat like creating a temp variable.

Learning Part II:
Then when I started learning constructor, the first thing that everyone told me - it helps in initializing the object members.

So, I used the list initialization again. And that throws errors. Why?

class Name{
 string personName;
 Name(){
   personName("Deb");
   }
};

But these alternative approaches work:

personName="Deb";

or

Name():personName("Deb");
  • For Part I, you might want to read [this](https://stackoverflow.com/a/1051468/9593596). For Part II, the error you get tells you that you're trying to use your member variable `personName` as a callable. This line hence doesn't initialize anything. `personName = "Deb"` in the body of your constructor assigns a new value to the member variable, that had previously been initialized to an empty string. The last snippet is one way to correctly initialize `personName` in the default constructor. – lubgr Jun 21 '18 at 09:07
  • @lubgr: Thanks, I'll go through those answers :) – Debasish Ray Mohapatra Jun 21 '18 at 09:10
  • @lubgr yeah, you are right. So, my question was why its not considering as an initialization instead of callable. I mean is there any logic behind it or c++ just behaves in that way. – Debasish Ray Mohapatra Jun 21 '18 at 09:18
  • `Name() : personName("Deb") { /* ... */ }´ is a syntax for initilializing member variables in the definition of the constructor. No real logic here, just the language rules. But inside of the curly braces of the constructor, you're writing ordinary code at function scope (special member function, but still a function). In this function (constructor) body, there are no special rules how member variables are treated. – lubgr Jun 21 '18 at 09:24
  • Thanks, that makes sense. – Debasish Ray Mohapatra Jun 21 '18 at 09:31
  • So the issue in part 1 with `string name = "Deb";` is that `name` first gets constructed empty, then gets assigned the value (except that compilers will optimise this away when they can). In part 2, your use of `personName="Deb";` has exactly the same issue: the compiler has implicitly constructed an empty `personName` [by effectively doing `Name():personName()`] which then gets assigned. In a more complex constructor, the compiler may find it difficult to optimise this away. `Name():personName("Deb");` is the "correct" efficient way to initialise members that need run-time values. – Gem Taylor Jun 21 '18 at 10:37
  • Thanks, @GemTaylor Yeah, it makes sense to me know after going through this post once more [here](https://stackoverflow.com/questions/9903248/initializing-fields-in-constructor-initializer-list-vs-constructor-body) and what you are talking about. – Debasish Ray Mohapatra Jun 21 '18 at 15:29
  • Confusingly, the inplace at-declaration `string personName={"unknown"}` is the recommended way to default-initialise data members that you are unlikely to want to initialise from parameters. These will be efficiently merged with any parameterised list constructors in a much better way than with constructor cascading. – Gem Taylor Jun 21 '18 at 16:14

0 Answers0