0

Being very new to c++, I have come across 2 different ways of using a constructor and I am confused on why one of them works, and the other doesen't.

class Journey {
    
protected:
    Coordinate start; 

// this constructor wont work
public:
    Journey(Coordinate startIn)
    {
        start = startIn;
    }
}
  • This constructor does not work as I get the error: constructor for 'Journey' must explicitly initialise the member 'start' which does not have a default constructor
public:
    Journey(Coordinate startIn): start(startIn){
    } 
  • whereas this constructor works perfectly fine
  • Also keep in mind "Coordinate" is just a class I created

I am not sure what the reason is as I thought the 1st way of initialising variables works for all cases, so I just need an explanation why this isn't the case here. I tried looking around for the answer without any success.

MohG
  • 25
  • 5
  • 1
    The 1st way *default initializes* all the member variables, then does an *assignment* to the variable in the body of the constructor. For critical path objects, that can be a significant performance hit. – Eljay Sep 17 '22 at 14:29
  • For the 1st way , the compiler gives me an error and can't run that code. Why is that? – MohG Sep 17 '22 at 15:07
  • Because the member variable's **type** does not have a *default constructor*. – Eljay Sep 17 '22 at 15:11
  • Right, so "start" was never initialised (with the default constructor) so it cannot take the value of the argument? – MohG Sep 17 '22 at 15:15
  • ...and it can't even compile. – Eljay Sep 17 '22 at 15:16
  • True, so for the 2nd way we initialise "start" somehow without a default constructor? How is it possible + what is the name for type of constructor? – MohG Sep 17 '22 at 15:18
  • @MohG *"for the 2nd way we initialise "start" somehow without a default constructor?"* -- yes, and it is not some mysterious "somehow". Look at your code. You explicitly invoke a specific constructor by having `start(startIn)` in your [member initialization list](https://stackoverflow.com/questions/1711990/what-is-this-weird-colon-member-syntax-in-the-constructor). – JaMiT Sep 17 '22 at 15:44
  • For the 2nd way, instead of constructing member variable `start` of type Coordinate with the (non-existent, in this case) *default constructor*, `start` is being constructed using Coordinate *copy-constructor*. – Eljay Sep 17 '22 at 16:31

1 Answers1

0

There's the concepts of initialization versus assignment. In the first constructor, if Coordinate had a default constructor (which means a constructor that takes no arguments) your start member variable would have been initialized by that constructor. Then, inside your constructor for Journey, you are assigning your start member variable using the argument passed in. That's doing double the work.

In the second Journey constructor, you are initializing the Coordinate member variable via it's constructor and you're done.

Anon Mail
  • 4,660
  • 1
  • 18
  • 21
  • So for the 1st one, I have to include a default constructor (constructor with no parameters from what I understood) to make "start" initialised, and after that, when I assign the start member variable using the argument passed in, it will have the value of the argument? – MohG Sep 17 '22 at 15:13
  • That will work, but it is less efficient than just doing what you did in the second Journey constructor. Why would you want to default construct and then assign when you can just constuct? – Anon Mail Sep 24 '22 at 15:19