My C++ 클래스에는 std :: vector 및 std :: array 인스턴스 변수를 삭제하려고하는 소멸자가 있습니다.std :: vector & std :: array를 삭제할 수 없습니까?
#include <iostream>
#include <vector>
#include <array>
int main()
{
std::array<int, 3> foo;
std::vector< std::array<float, 4> > vertices;
foo[0] = 1;
foo[1] = 2;
foo[2] = 3;
std::cout << foo[0] << std::endl;
delete foo;
delete vertices;
return 0;
}
메모리를 적절하게 확보하는 방법을 잘 모르겠습니다. 어떻게 이러한 변수를 삭제할 수 없습니까?
clang++ -std=c++11 -stdlib=libc++ -Weverything ccc.cpp
ccc.cpp:14:2: error: cannot delete expression of type 'std::array<int, 3>'
delete foo;
^ ~~~
ccc.cpp:15:2: error: cannot delete expression of type 'std::vector<std::array<float, 4>
>'
delete vertices;
^ ~~~~~~~~
ccc.cpp:18:2: warning: C++98 requires newline at end of file [-Wc++98-compat-pedantic]
}
^
당신은'new'로 아무 것도 할당하지 않았습니다, 왜'delete'로 어떤 것을 할당 해제하겠습니까? 이들은 자동 변수입니다. 범위를 벗어나면 자동으로 삭제됩니다. –
클래스의 구성원 인스턴스 변수 인 경우에도 마찬가지입니까? – ejang
예, 클래스 멤버에게는 숨겨진 새 항목이 없습니다. – chris