[C++] 부모 클래스가 템플릿 클래스일 경우 자식 클래스에서의 액세스

visualnnz·2024년 2월 25일
0

Cpp

목록 보기
7/7

부모 클래스가 템플릿 클래스일 경우 자식 클래스에서 부모 클래스로의 액세스 방법

  1. '부모클래스<T>: : 부모클래스 멤버함수 or 멤버변수'

예제 코드

template<typename T>
class Deque : public Queue<T>
{
   typedef Queue<T> Base;
   
   ...
   
   void PushFront(const T& item)
	{
		if (Base::IsFull())
			Base::Resize();

		if(Base::front_ == 0)
		{
			Base::front_ = Base::capacity_;
		}
		Base::front_ = (Base::front_ - 1) % Base::capacity_;
		Base::queue_[(Base::front_ + 1) % Base::capacity_] = item;
	}
    
    ...
    
}

  1. 'this-> 부모멤버'

예제 코드

template<typename T>
class Deque : public Queue<T>
{
   ...
   
   void PushFront(const T& item)
	{
		if (this->IsFull())
			this->Resize();

		if(this->front_ == 0)
		{
			this->front_ = this->capacity_;
		}
		this->front_ = (this->front_ - 1) % this->capacity_;
		this->queue_[(this->front_ + 1) % this->capacity_] = item;
	}
    
    ...
    
}

0개의 댓글