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(); // this outputs "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 ?
===========
Tested that the pointer is actually nullptr.