소프트웨어/c++

    함수, 함수 포인터, 함수 객체를 이용한 정수 출력 / print int type using function, function pointer, function element

    함수 호출 연산자 오버로딩은 객체를 함수처럼 동작하게 하는 연산자입니다. print1 : 함수 호출point2 : 함수 포인터point3 : 함수 객체 #include using namespace std; struct FuncObject { public: void operator() (int arg) const { cout

    const 벰버 함수와 비 const 멤버 함수

    const 멤버 함수는 멤버 함수 내에서 객체의 멤버 변수를 변경하지 않는다는 것을 보장하는 함수입니다. 따라서 const 객체는 const 멤버 함수만 호출할 수 있습니다. const 멤버 함수에서 멤버 변수를 변경하면 컴파일 에러입니다. 사실 자신의 멤버를 변경하지 않는 멤버 함수는 모두 const 멤버 함수여야만 합니다. function의 맨 뒤에 const가 붙으면 그 function은 const function이고,변수 앞에 const가 붙으며 그 변수가 const이다. #include using namespace std; class Point { int m_x; int m_y; public: Point(int _x = 0 , int _y = 0) : m_x(_x), m_y(_y) { } int..

    flood fill을 구현해보았습니다. - 다른 분의 코드에서 확장

    flood fill을 구현해보았습니다. - 다른 분의 코드에서 확장

    이번에 소개해드릴 알고리즘은 Flood fill입니다. 한글 위키피디아 : http://ko.wikipedia.org/wiki/%ED%94%8C%EB%9F%AC%EB%93%9C_%ED%95%84영문 위키피디아 : http://en.wikipedia.org/wiki/Flood_fill 그리고 참고한 소스에서 클래스 모양까지 만든 것입니다. /* * nonrecurcive */ #include #include class FloodFill { typedef struct StackType { int x; int y; } Stack; private: int m_row, m_col; int *m_map; public: FloodFill() : m_row(0), m_col(0), m_map(NULL) { } ~Flo..

    C++에서 클래스를 지역변수로 만들면서 constructor호출하기

    C++에서 클래스를 지역변수로 만들면서 constructor호출하기

    그동안은 class의 constructor를 사용하기 위해서 동적할당을 사용하곤 했습니다.지역변수를 선언할 때 constructor를 부르는 생각 조차 못했었죠. 그런데 회사에서 팀장님이 코드리뷰를 해주시면서 알려주셨습니다. 아주 간단한 방법이더라구요 코드입니다. #include class Test { public: Test() { } Test(int data) { printf("Init data : %d\n", data); } }; int main() { Test test(4); return 0; } 결과 : Init data : 4

    비트연산 테스트 코드

    비트연산 테스트 코드

    비트연산을 할 일이 생겼는데 감을 잃어서 다시 확인차 해보았습니다. #include int main() { int a = 64; printf("%d & 0xFF = %d\n", a, a & 0xFF); printf("12 & 3 = %d\n", 12 & 3); printf("12 & 7 = %d\n", 12 & 7); printf("12 & 7 = %d, %d > 1 = %d\n", 12 & 7, 12 & 7, (12&7) >> 1); return 0; } 결과는 아래와 같습니다. 64 & 0xFF = 6412 & 3 = 012 & 7 = 412 & 7 = 4, 4 > 1 = 2

    좌선 알고리즘이란

    퀴즈를 푸는데 미로 찾기를 푸는 중이였습니다. http://poj.org/problem?id=3083 그런데 좌선법이란것을 듣고선 저는 당연히 좌-> 상-> 우-> 하 이렇게 돌거라고 생각했지만 실제 동작은좌측 벽을 따라서 가는 것이라고... 그래서 코드를 갈아 엎었다는...ㅜㅜ https://www.google.co.kr/?gfe_rd=cr&ei=WINUU7XDCOK8iAelsYGYDQ#newwindow=1&safe=off&q=%EC%A2%8C%EC%84%A0%EB%B2%95+%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98 우선법보기 클릭 (동작은 동일합니다.)

    error, relocation 0 has invalid symbol index 11 에 대해서

    1. 해결방법int main() {/* implements */ return0;} 을 추가한다. main이 없다고 나오는 에러이다. 2. 에러 로그ㅌㅌㅌ@ㅌㅌㅌ:~/childrenOfTheCandyCorn$ g++ Maze.cpp /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linu..

    #define 속에 do while (0) 문장의 의미

    #define 속에 do while (0) 문장의 의미

    이 질문의 시작은 딱 이 문제이다.http://stackoverflow.com/questions/923822/whats-the-use-of-do-while0-when-we-define-a-macro ( 구글 검색 결과 : https://www.google.co.kr/?gfe_rd=cr&ei=WINUU7XDCOK8iAelsYGYDQ#newwindow=1&safe=off&q=c%2B%2B+define+do+while+0 )(예시)#define TEST(x) do { \ printf("%d\n", x); \} while (0) 위 주소에서 얻을 수 있는 정보는Macro는 정확히 할당이 되고, 따라서 { } 으로 묶으면 (scope이 생기기 때문에:제 생각) 안된다. 입니다. 마지막으로 이 예제를 보면 확실합니..