본문 바로가기

Programming/C & C++43

operator== issue in inheritance 클래스 A 와 B가 있는데 A 는 B를 상속한다. 만약 클래스 A에 operator== 를 구현하고자 한다면 상속된 B의 멤버 변수에 대한 비교도 포함을 시켜야 한다. 한가지 방법으로는 클래스 A안에서 B의 멤버변수들을 모두 비교해볼수도 있지만, 이는 좀 껄쩍지근하다. 게다가 B의 멤버변수가 많아지면, 타이프하기 구찮아지고 빼먹을 확률도 높다. 두번째 방법으로는 ParentClass::operator== 를 직접 호출하는게 있다. 우연히 발견한건데 이미 많은 사람들이 알고 있을지도 ... ㅋㅋ 아래의 코드를 보면 알수 있다. #include using std::cout; using std::endl; class Parent { public: bool operator== (const Parent& obj).. 2009. 9. 10.
operator == in std::pair pair 자료형으로 선언한경우 operator == 를 사용하려면 이에 대해 오버로딩을 해줘야 할것 같지만 pair 안에서 자체적으로 first 인자와 second 인자에 대해 각각 == 을 적용시킨후 결과를 반환한다. 아래의 소스를 보면 그 의미가 좀 더 구체적으로 다가온다. Compilation 이 나는 이유는 TEST class 에 == 를 호출하려고 하는데 정의가 되어 있지 않기 때문이다. #include using std::cout; using std::endl; class TEST { public: int num; }; // end - TEST int main() { std::pair p1, p2; p1.first = 2; p1.second = 3; p2.first = .. 2009. 9. 10.
istream operator >> overriding into template class #include #include template class StreamTest { private: STRING name_; STRING title_; friend std::istream& operator>>( std::istream&, StreamTest&); public: void print() { std::cout ( istream&, const StreamTest) int main() { StreamTest test; std::cin >> test; test.print(); return 0; } 특정 template 클래스에 overriding 하기 위한 참조 코드. 지금까지 인터넷 검색 및 지인에게 물어본 결과, template class 라도 operator>> 를 overriding 하는 경우.. 2009. 7. 31.
Linux 와 Window 에서 현재시간 값 얻어오기 #include #if defined(WIN32) # ifndef _WINSOCKAPI_ # define _WINSOCKAPI_ # endif # include #else #include #endif int main() { char time_string[40]; #ifdef WIN32 SYSTEMTIME currentTime; ::GetLocalTime(&currentTime); sprintf(time_string, "%4ld-%02ld-%02ld %02ld:%02ld:%02ld", currentTime.wYear, currentTime.wMonth, currentTime.wDay, currentTime.wHour, currentTime.wMinute, currentTime.wSecond); #else .. 2009. 4. 14.