Given this code:
#include <iostream>
class Foo {
    public:
        Foo(const std::string& label) : label_(label) {}
        void print() {
            std::cout << label_;
        }
    private:
        const std::string& label_;
};
int main() {
    auto x = new Foo("Hello World");
    x->print();
}
I get
Hello World!
when I run it. If I modify it like this:
// g++ -o test test.cpp -std=c++17
#include <iostream>
class Base {
    public:
        Base(const std::string& label) : label_(label) {}
        void print() {
            std::cout << label_;
        }
    private:
        const std::string& label_;
};
class Derived : public Base {
    public:
        Derived(const std::string& label) : Base(label) {}
};
int main() {
    auto x = new Derived("Hello World");
    x->print();
}
I still get:
Hello World
but if I modify it like this:
// g++ -o test test.cpp -std=c++17
#include <iostream>
class Base {
    public:
        Base(const std::string& label) : label_(label) {}
        void print() {
            std::cout << label_;
        }
    private:
        const std::string& label_;
};
class Derived : public Base {
    public:
        Derived() : Base("Hello World") {}
};
int main() {
    auto x = new Derived();
    x->print();
}
I do not get any output. Can anyone explain this to me? I am compiling the program like this:
g++ -o test test.cpp -std=c++17
This is on Mac if it makes a difference.
 
     
    