인프런 커뮤니티 질문&답변
타입 불일치 오류
작성
·
264
0
#include <iostream>
#include <cassert>
#include <initializer_list>
class IntArray {
private:
unsigned int m_length{ 0 };
int* m_data = nullptr;
public:
IntArray(unsigned length)
:m_length(length) {
m_data = new int[length];
}
// ERROR: C4267 - 'argument': conversion from 'size_t' to 'unsigned int', possible loss of data
IntArray(const std::initializer_list<int>& list)
:IntArray(list.size()) {
unsigned int count{ 0 };
for (auto& element : list) {
m_data[count++] = element;
}
}
~IntArray() {
delete[] this->m_data;
}
};
int main() {
IntArray int_array{ 0,1,2,3,4 };
}
안녕하세요.
MSVS debug x64 에서 빌드하면 이런 오류가 나옵니다.
----
C4267 : 'argument' - conversion from 'size_t' to 'unsigned int', possible loss of data
----
dubug x86 에서는 오류가 안나오네요.
이유가 궁금합니다. 감사합니다.
퀴즈
사용자 정의 타입에 대해 연산자 오버로딩을 하는 주된 목적은 무엇일까요?
코드 실행 속도를 빠르게 하기 위해
사용자 정의 타입을 내장 타입처럼 자연스럽게 사용하기 위해
객체의 메모리 관리를 자동화하기 위해
클래스의 상속 관계를 정의하기 위해
답변 1
0
안녕하세요
Warning이 뜨는 이유는 라인 마지막에 있는 list.size() 에서
std::size의 반환 값이 size_t 형이라 그렇습니다.
size_t는 x64일때 unsigned __int64(8byte), x86에서는 unsigned int(4byte)로 typedef되어있습니다.
그래서 x64에서 빌드하면 8byte 값이 4byte로 conversion되면서 소실될 수 있다고 Warning이 나오게 됩니다.
이후 강의에 size_t가 자세히 언급됩니다.





