jeudi 13 décembre 2018

unique_ptr field with inaccessible object constructor

I have following codes, not sure how unique_ptr is initialized.

a.h

#ifndef EXPERIMENTAL_USERS_ALOUA_CPP_PROTECTED_CTOR_INTIALIZE_A_H_
#define EXPERIMENTAL_USERS_ALOUA_CPP_PROTECTED_CTOR_INTIALIZE_A_H_

#include <iostream>
#include <memory>

class A {
 public:
  void help() { std::cout << "help ! \n "; }

 protected:
 A() = default;
};

#endif  // EXPERIMENTAL_USERS_ALOUA_CPP_PROTECTED_CTOR_INTIALIZE_A_H_

main.cc

#include <iostream>
#include <memory>
#include "experimental/users/aloua/cpp/protected_ctor_intialize/a.h"
struct B {
  std::unique_ptr<A> a;
  // the default constructor
  // A aa;
};

int main() {
  B b;
  b.a->help();
}

how can the program compile and run without any issue ? is it because of std::unique_ptr<A> a is zero initialized according to value initialization and no actual constructor is actually called ?

Aucun commentaire:

Enregistrer un commentaire