상속된 템플릿 클래스의 연산자 오버로딩(Operator Overloading)

C++ 에서 상위 크래스로 부터 상속받은 하위 클래스에서 연산자재정의(Operator Overloading) 하는 방법을 알아보자.
난해한 분분이 아니지만 가끔 해당부분을 구현하려고 생각하면 막막 할때가 있다.
C++ 초급자에겐 무척 이해하기 어려울 지도 모르겠다.

예제 소스코드는 아주 짧지만 눈에 잘 보이지 않는 함점을 가지고 있다, 또한 Library 가 풍부하기 때문에 Operator Overloading을 직접 구현하는 경우는 잘 없기 때문에 익숙하지 않아서 막상 코딩을 하려면 막막하기만 하다.

class WRECT는 RECT 클래스를 변수로 가진다.(MFC에서 RECT는 사각형을 조작 하기 쉽도록 구현된 클래스이다)
WRECT에는 변수로 ID를 지정하고 있다.
클래스의 구현 목적은 윈도우창의 ID와 그에 해당하는 화면사각형의 크기를 임의로 조작 하고 관리하기 위해 고안된 것이다.

 
그리고 class CRectIDArray는 CArray를 상속 받아서    WRECT 클래스의 배열을 할당한다.
class CRectIDArray:public CArray<WRECT,WRECT> 를 보면 WRECT관련 배열을 할당하고 있음을 알 수 있다.
CArray는 동적 배열을 하당하고 쉽게 관리하는 기능을 가지고 있다.
그런데 여기서 조금 어려운 부분은 CArray가 Template Class를 사용하고 있으므로 익숙하지 않은 개발자에게 좀 어렵게 보인다.

 

class WRECT
{
public:
	WRECT()
	{
		ID=0;
		m_rect.left=0;
		m_rect.top=0;
		m_rect.right=0;
		m_rect.bottom=0;
	};
	WRECT(WORD id)
	{
		ID=id;
		m_rect.left=0;
		m_rect.top=0;
		m_rect.right=0;
		m_rect.bottom=0;
	};
	WRECT(WORD id,int x1,int y1,int x2,int y2)
	{
		ID=id;
		m_rect.left=x1;
		m_rect.top=y1;
		m_rect.right=x2;
		m_rect.bottom=y2;
	};
	WORD ID;
	RECT m_rect;
};
class CRectIDArray:public CArray<WRECT,WRECT>
{
public:
	CRectIDArray(){};
    int Add(WORD id )
	{
		return CArray<WRECT,WRECT>::Add( WRECT(id));
	};
	int Add(WORD id ,int x1,int y1,int x2,int y2)
	{
		return CArray<WRECT,WRECT>::Add( WRECT(id,x1,y1,x2,y2));
	};
	WORD& operator []( int nIndex )
	{
		WRECT p_r=CArray<WRECT,WRECT>::operator[](nIndex );
		return p_r.ID;
	};
    WORD operator []( int nIndex ) const
	{
		WRECT p_r=CArray<WRECT,WRECT>::operator[](nIndex );
		return p_r.ID;
	};
	WORD GetAt(int nIndex)const
	{
		return operator[](nIndex );
	};
	WORD GetAt( int nIndex ,RECT& pRec)
	{
		WRECT p_r=CArray<WRECT,WRECT>::GetAt(nIndex);
		pRec =p_r.m_rect;
		return p_r.ID;
	};
};

자 하나씩 확인해보자

CArray Class Members 를 보면
Attributes    

    GetSize Gets the number of elements in this array.
    GetUpperBound Returns the largest valid index.
    SetSize Sets the number of elements to be contained in this array.

Operations

    FreeExtra Frees all unused memory above the current upper bound
    RemoveAll Removes all the elements from this array. .

…………………….
Operators
  operator [] Sets or gets the element at the specified index.

와 같이 구성되 있는데

1. 부모클래스 연산자를 상속된 자식클래스에서 재정의

    WORD operator []( int nIndex ) const 와 같이 하위크래스에서 다시 정의 하였다.

    함수코드 내부에서 부보클래스의 연산자를 호출하기 위해서는 “부모클래스::operator []( int nIndex )”와 같이 호출하면 된다. 그런데 상위클래스가 Template 클래스를 사용하고 있으므로 하위클래스의 코드구현 부분에서    WRECT p_r=CArray<WRECT,WRECT>::operator[](nIndex ); 와 같이 호출 해야 한다.

2. 연산자 중복
    CRectIDArray의 코드를 보면 CArray<WRECT,WRECT> 를 상속 받고   WRECT와 관련된 어떠한 행동도 없다. 조금 이상하게 보이겠지만 여긴엔 연산자 중복의 개념이 숨어 있다. 

    operator []( int nIndex ) 는 CArray에도 있지만 상속된 CRectIDArray에도 있다, 그런데 두 클래스는 값을 반환하는 Type이 다르다.

    TYPE& operator []( int nIndex );   

    WORD CRectIDArray::operator []( int nIndex ) const
    WRECT CArray<WRECT,WRECT>::operator[](nIndex )
   이는 연산자가 중복되어 있으므로

 

void main()

{   

     WRECT mW[2];

     mW[0].ID=10;

     mW[1].ID=100;

   

     CRectIDArray   mArr; ///WRECT 를 배열로 추가한다

     mArr.Add(mW[0]);

     mArr.Add(mW[0]);

     mArr.Add(mW[1]);

     mArr.Add(mW[1]);

    

     mW[2];를 선언해서  WRECT 를 두개를 할당 했지만

     mArr는 4개의 WRECT 가 추가되어 있다.

:    int sz  =mArr.GetSize(): 를 실행하면 sz는 4를 반한한다.

   

      WORD id=mArr[0];//를 실행하면 WORD&CRectIDArray:: operator []( int nIndex ) 가 실행되어 id=10 이된다

      WRECT wr=mArr[0];//를 실행하면 CArray<WRECT,WRECT>::operator[](nIndex )가 실행되어 wr에는 WREC가 할당된다.

    

     }

 
 

 

 

 

“상속된 템플릿 클래스의 연산자 오버로딩(Operator Overloading)”의 75개의 댓글

  1. 핑백: Panzer Arms AR-12 PRO Shotgun AR Twelve PRO 12ga

  2. 핑백: giải mã giấc mơ thấy rắn hổ mang

  3. 핑백: Online Klundert Dating Club

  4. 핑백: mơ thấy xương người

  5. 핑백: mơ bị đứt tay

  6. 핑백: DevOps

  7. 핑백: Digital transformation consultants

  8. 핑백: mơ bị sét đánh

  9. 핑백: mơ thấy lợn đẻ

  10. 핑백: best quality best synthetic wigs under $59

  11. 핑백: 안전공원

  12. 핑백: what is web hosting

  13. 핑백: nằm mơ thấy mình thắp hương

  14. 핑백: nằm mơ thấy hoa sen

  15. 핑백: nằm mơ thấy ngựa đánh số gì

  16. You actually make it seem really easy along with your presentation but I to find this topic to be
    really one thing which I feel I’d never understand. It seems too
    complicated and very huge for me. I am having
    a look ahead on your next put up, I’ll attempt to get the grasp of it!

  17. I’m not positive the place you’re getting your info, however great topic.
    I must spend a while studying more or working out more. Thanks for excellent information I used to
    be searching for this info for my mission.

  18. Hey would you mind letting me know which hosting
    company you’re utilizing? I’ve loaded your blog in 3 completely different
    internet browsers and I must say this blog loads a lot quicker then most.
    Can you recommend a good hosting provider at a honest price?
    Thanks, I appreciate it!

  19. Thanks for another excellent article. Where else may anybody get that kind of info in such a
    perfect means of writing? I have a presentation next week,
    and I am on the search for such information.

  20. I am curious to find out what blog platform you have
    been working with? I’m having some small security issues with my
    latest blog and I’d like to find something more safe. Do you have any solutions?

  21. Its such as you read my mind! You appear to know a lot approximately this,
    such as you wrote the guide in it or something. I feel that
    you could do with a few percent to power the message home a bit, but
    other than that, that is excellent blog. A fantastic read.
    I’ll definitely be back.

  22. 핑백: online wig stores

  23. I like the valuable info you provide in your articles. I’ll bookmark your weblog and check again here regularly.
    I am quite certain I’ll learn many new stuff right here!

    Good luck for the next!

  24. 핑백: cc cvv shop

  25. I do not know if it’s just me or if perhaps everybody
    else experiencing issues with your site. It seems like some of the text within your content
    are running off the screen. Can someone else please comment and let me know if this
    is happening to them as well? This may be a issue with my internet
    browser because I’ve had this happen before.
    Cheers

    My blog :: s128 login

  26. I’m honored to obtain a call from a friend immediately he
    observed the important points shared on the site. Reading through your blog article is a real fantastic experience.
    Thank you for thinking about readers just like me, and I want for you the best of achievements for a professional in this
    area.

    Also visit my website: carquest mn locations

  27. I know this if off topic but I’m looking into starting my own weblog and was wondering what all is
    needed to get setup? I’m assuming having a blog like yours would cost a pretty penny?
    I’m not very internet smart so I’m not 100% sure. Any tips or advice
    would be greatly appreciated. Thanks

    my web page – link alternatif s128

  28. I have been browsing link poker online more than three hours these days, yet I by no
    means found any interesting article like yours.
    It’s beautiful worth sufficient for me. Personally,
    if all web owners and bloggers made just right content material as you did, the internet might be a lot more helpful than ever before.

  29. Hey! I know this is somewhat off topic but I was
    wondering which blog platform are you using for this website?
    I’m getting tired of WordPress because I’ve had issues with hackers and I’m looking at options
    for another platform. I would be great if you could point me in the direction of a good platform.

    Also visit my web-site idn poker online

  30. I think everything published was very logical. But, consider this, what if you composed a catchier title?
    I am not suggesting your information is not solid,
    however what if you added a headline to possibly grab folk’s attention? I mean 상속된 템플릿 클래스의 연산자 오버로딩(Operator Overloading) | 기록은 자산 is kinda plain. You
    ought to glance at Yahoo’s front page and watch how they create news headlines to
    get viewers to open the links. You might add a video or a related pic or two to get people excited about what you’ve written. Just my opinion, it
    might bring your website a little bit more
    interesting.

    Feel free to surf to my web blog – idn live deposit pulsa

  31. Howdy! This post could not be written any better! Going
    through this article reminds me of my previous roommate! He continually kept preaching about this.
    I most certainly will send this information to him. Pretty sure
    he’s going to have a great read. Many thanks for sharing!

답글 남기기