以下の2番目で選択したもの以外GetJ()
に、クラスのメンバー関数にアクセスするためのより簡単な方法はありますか?Derived
std::cout
#include <iostream>
#include <memory>
class Base
{
int i;
public:
Base(int k) : i(k) {}
int GetI() { return i; }
};
class Derived : public Base
{
int j;
public:
Derived(int u) : Base(10) { j = u; }
int GetJ() { return j; }
};
int main()
{
std::unique_ptr<Base> uptr(new Derived(5));
std::cout << uptr->GetI() << std::endl;
std::cout << static_cast<Derived*>(uptr.get())->GetJ() << std::endl;
}