컴파일 에러 C2662


'this' 포인터를 'const XXXX'에서 'XXXX &'(으)로 변환할 수 없습니다.



http://simplesolace.tistory.com/entry/const-%EB%A9%A4%EB%B2%84-%ED%95%A8%EC%88%98



대입 생성자에서 넘겨받은 레퍼런스의 멤버 함수를 호출했더니 const 함수에서는 일반 함수를 호출할 수 없다나 뭐래나...

http://comfun.tistory.com/entry/error-C2662-PointGetY-this-%ED%8F%AC%EC%9D%B8%ED%84%B0%EB%A5%BC-const-Point%EC%97%90%EC%84%9C-Point-%EC%9C%BC%EB%A1%9C-%EB%B3%80%ED%99%98%ED%95%A0-%EC%88%98-%EC%97%86%EC%8A%B5%EB%8B%88%EB%8B%A4


그래서 호출한 함수를 const 함수로 변경하니 잘 된다...


대입 생성자


SomeClass &SomeClass ::operator = (const SomeClass &r_)
{
    if (&r_ == this) return *this;

    Data = *(r_.getData());

    return *this;
}


= 호출된 함수를 아래와 같이 변경하니까 아주 잘된다.


(변경전)

    PTR getData() { return &Data; }

(변경후)

    const PTR getData() const { return &Data; }




* 뮤텍스
std::mutex        mtx;

mtx.lock();
mtx.unlock();


std::mutex        mtx;

std::lock_guard lg(mtx)


std::recursive_mutex mtx;
void ThreadFunc(int nID, int& nVal)
{
    for (int i = 0; i < 5; ++i)
    {
        std::lock_guard lg(mtx);
 
        std::cout << "Value of " << nID << " -> " << nVal << std::endl;
        ++nVal;
    }
}



* 조건변수 (스레드의 흐름제어)


std::condition_variable cv;

cv.wait()            // 인자로 unique_lock 뮤텍스를 넣는다.
                    //이중 검사를 위해 두번째 인자로 notify하는 곳에서 변경하는
                    // flag를 리턴하는 람다 함수를 넣을 있다.

    std::unique_lock<std::mutex> ul(readyMutex);
    cv.wait( ul, []{return readyflag;});


cv.notify_one() / cv.notify_all()
                    // 해당 조건 변수를 기다리고 있는 스레드중 무작위 한개를 깨우거나
                    // 모든 스레드를 깨우거나
                    // flag를 변경하기 위해서 lock_guard된 mutex로 lock을 사용


    {
        std::lock_guard<std::mutex> lg(readyMutex);
        readyflag = true;
    }
    cv.notify_all()



std::condition_variable cv;
std::mutex readyMutex;
                    // 위 두 변수를 사용

'프로그래밍 > 언어_스크립트' 카테고리의 다른 글

오랜만에 C++ -  (0) 2017.10.30
오랜만에 C++ - const, static, explicit, mutale  (0) 2017.09.11
오랜만에 C++ - 파일 처리  (0) 2017.09.10
오랜만에 C++ - chrono  (0) 2017.09.02
오랜만에 C++ - const의 위치  (0) 2017.09.02

*** const

1. const double PI = 3.14;        // 변수를 상수화

PI = 1234;                        // 컴파일 오류


2. const int val;                // 포인터가 가르키는 데이터를 상수화

val = 20;                        // 컴파일 오류


3. int *const pN = &n1;            // 포인터 자체를 상수화

pN = 20;                        // ok
pN = &n2;                        // 컴파일 오류


4. func() const {}                // const 멤버 함수, 이 함수를 통해서 멤버변수의 변경을 허용되지
                                // 상수화된 함수의 호출 불가능, 멤버 변수의 포인터 리턴 불가능


5. const 객체                    // 생성된 인스턴스의 멤버 변수의 변경 불가능
                                // 상수화된 멤버 함수만 호출 가능
                                //

6. void func() const {}            // 오버로딩을 통해서 const 객체에서 함수를 호출 가능하다
                                // 비 상수화 함수의 우선순위기 높다.





** 멤버 초기화는 생성자의 몸체 보다 먼저 실행된다.

const 멤버 변수(2.번 스타일)를 초기화할 수 있다.


** static

객체 생성 이전에도 접근이 가능하다. (public 멤버일 경우)
객체의 멤버로 존재하는 것이 아니라 클래스 내에서 직접 접근할 수 있는 권한이 부여된 것이다.



** explicit

묵시적인 호출을 허용하지 않는다.

class A = 10;                    // 묵시적으로 class A(10); 와 같이 변환되지만 키워드에 의해 컴파일 에러



*** mutable

상수화된 멤버 함수에서도 수정을 가능하도록 멤버 변수를 선언한다.






파일을 스트림으로 처리하기 위해서..


#include <fstream>


std::ifstream : 입력용 파일

std::ofstream : 출력용 파일


open 모드

std::ios::in

out

binary

ate

append

trunc


tellp / seekp 입력 파일에서의 동작

tellg / seekg 출력 파일에서의 동작



seekg(0, std::ios::end);

size = infile.tellg();

seekg(0, std::ios::beg);


size : 파일 크기





before = outfile.tellp();

outfile.write();

temp = outfile.tellp();

outsize = temp - before;


outsize : 저장한 바이트 수


infile.gcount() : 입력 파일에서 읽은 바이트 수



#include <chrono>

std::chrono::system_clock::time_point starTime;
std::chrono::system_clock::time_point endTime;



* 특정 시간 동안

std::chrono::seconds three(3);

startTime = std::chrono::system_clock::now();

std::this_thread::sleep_for(three);

endTime = std::chrono::system_clock::now();

std::chrono::seconds during = std::chrono::duration_cast<std::chrono::seconds>(endTime - startTime);

std::cout << during.count() << std::endl;




* 특정 시간 이후에..

std::chrono::seconds five(5);

startTime = std::chrono::system_clock::now();

std::this_thread::sleep_until( startTime + five );

endTime = std::chrono::system_clock::now();

during = std::chrono::duration_cast<std::chrono::seconds>(endTime - startTime);

std::cout << during.count() << std::endl;



* sleep

#include <thread>

#include <chrono>


std::this_thread::sleep_for(std::chrono::seconds())


std::chrono::seconds()

std::chrono::mileseconds()

microseconds()

hours()

minutes()

nanoseconds()

high_resolution_clock()



+ Recent posts