티스토리 뷰

int main()
{
	Derived1 d1;
	
	Base *base = &d1;
	
	auto *base_to_d1 = dynamic_cast<Derived1*>(base);
	cout << base_to_d1->m_j << endl;
	
	cout << d1.m_j << endl;
	
	return 0;
}

// 123
// 123

 

static_cast는 구글 참고

 

#include<iostream>

class Base
{
	public:
	Base() {}
	
	friend std::ostream& operator << (std::ostream &out, const Base &b)
	{
		return b.print(out);
	}
	
	virtual std::ostream & print(std::ostream& out) const
	{
		out << "Base";
		return out;
	}
};

class Derived : public Base
{
	public:
	Derived() {}
	
	virtual std::ostream & print(std::ostream& out) const override
	{
		out << "Derived";
		return out;
	}
};

int main()
{
	Base b;
	std::cout << b;
	
	Derived d;
	std:: cout << d;
	
	return 0;
}

// BaseDerived
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함