I'm trying to pass a pointer that points to a Derived class to a Base class constructor when creating an object. Here is the scenario:
class Base
{
public:
// Default constructor
Base(Base* base_ptr = nullptr)
{
cout << base_ptr << endl;
base_ptr->foo();
}
virtual void foo() { cout << "Base foo" << endl; }
};
class Derived : public Base
{
public:
Derived* derived_ptr;
Derived() : derived_ptr(this), Base(this)
{
cout << derived_ptr << endl;
derived_ptr->foo();
}
virtual void foo() override { cout << "Derived foo" << endl; }
};
In main.cpp:
...
Derived derived;
...
1 . Why does it print out the exact same memory for base_ptr and derived_ptr, but a different method of Foo() is called for each constructor ?
2 . If in initializer list for Derived() instead of Base(this) i do Base(derived_ptr), why does it pass in some junk ?