0

Having this struct:

struct Coordinates
{
    int x;
    int y;

    Coordinates(int x_axis, int y_axis)
    {
        x = x_axis;
        y = y_axis;
    }

    Coordinates()
    {
        std::cout << " CALL " << std::endl;
    }
};

And having this class:

class Character
{
public:
    bool canSpeak;
    int hp;
    float gold;
    Coordinates position;

    Character();
};

I want to understand one thing (I know that the class' members initialize in the order in which they are declared). So, if I would do this:

Character::Character() : position(25, 32), canSpeak(1), hp(position.y), gold(position.x)
{
    std::cout << gold << " " << hp << std::endl;
}

Both hp and gold would contain garbage values but I would like to see the chronological axis in which this happens because before the curly brackets, all class members should be initialized (this means they do need to initialize between : and { , and if they initialize in the order in which appear, how can position.y and position.x be 'used'?

I was expecting the Coordinates::Coordinates() to be called before the Coordinates::Coordinates(int x_axis, int y_axis) in order for the compiler to be able to use position.x and position.y but this doesn't happened. So I don't understand what happens chronological.

enter image description here

Cătălina Sîrbu
  • 1,253
  • 9
  • 30
  • Related post: https://stackoverflow.com/a/1242835/4123703 – Louis Go Aug 07 '20 at 05:16
  • 2
    The order of the member in class declaration matters, not the order after the `: ` – RoQuOTriX Aug 07 '20 at 05:18
  • Unrelated suggestion: using coordiante to decide hp and mp seems like unintuitive and it might become a maintenance black hole in the future. – Louis Go Aug 07 '20 at 05:18
  • I've update my question with a picture. Is it really duplicate? Because I didn't find the answer I was looking for in the other questions. @RoQuOTriX I can see that you even didn't read my entire question :) _I want to understand one thing (I know that the class' members initialize in the order in which they are declared)_ – Cătălina Sîrbu Aug 07 '20 at 05:38
  • 1
    Its not the problem in the Coordinates class but in the Character class, where the order mattters. Because you want to use the members of Character to initialize Character, not that one of Coordinate (the values are hidden behind Coordinate, which is part of Character). If you put Coordinates member at the top in Character it will compile and run sucessful, but I couldn't post my answer... – RoQuOTriX Aug 07 '20 at 05:42
  • @RoQuOTriX but my question is how can I acces some members of Coordinates if in Character class, the member position initialize after hp and gold? Sory if I don;t understand but I think I wasn't clear enough (or it's just me that don't understands) – Cătălina Sîrbu Aug 07 '20 at 06:01
  • 1
    Compiler might still get the value from the momery (though not initialized). I'll assume it's undefined behavior since `position` is not initialized. But I didn't dig into standard. – Louis Go Aug 07 '20 at 06:05
  • What do you expect the values to be in position, if you did not initialize it and then want to use them to initialize hp and gold? Maybe we are both talking above our heads and don't understand what the other one means. What I want to say is. 1. member has to be `Character position` then it gets initialized first, after that you can safely use the members of position itself – RoQuOTriX Aug 07 '20 at 06:06
  • @LouisGo this was the answer I was actually looking for – Cătălina Sîrbu Aug 07 '20 at 06:06
  • @CătălinaSîrbu ah now I understand sorry, but I think this is UB – RoQuOTriX Aug 07 '20 at 06:07
  • ok, thank you very much ! – Cătălina Sîrbu Aug 07 '20 at 06:07
  • 1
    @LouisGo my compiler gave me warnings: https://godbolt.org/z/sE1K9j – RoQuOTriX Aug 07 '20 at 06:08
  • @RoQuOTriX I still got warnings about the order but I was expecting an error like "cannot find reference to `position.x`" . Or something like "cannot find `reference.x`, aka "not declared yet" – Cătălina Sîrbu Aug 07 '20 at 06:09
  • Don't ignore warnings because they are as important as errors and might be more dangerous because it let you run it. That's why some compilers have default settings to "treat warnings as errors". – Louis Go Aug 07 '20 at 06:10
  • @CătălinaSîrbu it isn't an error, because the "reference" exists in memory (still has garbage values in it) and it is a "valid" c++ program – RoQuOTriX Aug 07 '20 at 06:12
  • @RoQuOTriX talking again about initialising list, should I treat the constructor like "before `:` all members were declared " ? (but then the question raises how could `position` be declared without calling any constructor?) _i said that position was declared thinking about the reference from the memory which exists somehow_ – Cătălina Sîrbu Aug 07 '20 at 06:15
  • I think you are confused. Declaration is not initialization (in C/C++). Character has one constructor with a "well-defined" initialization list (all members are initialized), BUT not all values which are used in this initialization list are "well-defined" as for `position.x` e.g. The default constructor of `Coordinates()` is never used and so leaving x and y (memory)-blank – RoQuOTriX Aug 07 '20 at 06:19

0 Answers0