home.social

#constructors — Public Fediverse posts

Live and recent posts from across the Fediverse tagged #constructors, aggregated by home.social.

fetched live
  1. CW: Ще одна підстава C++
    class Point {
        int x, y;
    public:
        Point(int a) : y(a), x(y) {}
    };
    

    Спочатку здається що все добре у цьому коді, але насправді не так, тому що спочатку x проініціалізується значенням y, а вже потім y значенням аргументу a. Вийде що x буде мати випадкове значення. У цьому прикладі все може бути замітно зразу оскільки це тільки маленький приклад, але в реальності не все так просто. Добре що компілятори вміють про це попереджувати, якщо вказати прапорець -Wall.

    <stdin>: У конструкторі «Point::Point(int)»:
    <stdin>:2:9: попередження: «Point::y» буде ініціалізовано після [-Wreorder]
    <stdin>:2:6: попередження:   «int Point::x» [-Wreorder]
    <stdin>:4:2: попередження:   коли він ініціалізується тут [-Wreorder]
    <stdin>:4:25: попередження: член «Point::y» використовується без ініціалізації [-Wuninitialized]
    

    #cpp #cxx #class #stuct #constructors #initializelist #ООП #ініціалізація #конструктори

  2. @nebu how would you distinguish #constructors in #java if the fields had the same type? Anyway, the point was you can create your own constructor that copies all records but a ‘specified’ few. If you need 2^n permutations, then you mine as well use the full constructor every time.

  3. @nebu how would you distinguish #constructors in #java if the fields had the same type? Anyway, the point was you can create your own constructor that copies all records but a ‘specified’ few. If you need 2^n permutations, then you mine as well use the full constructor every time.