다음과 같은 것이 있습니다 :MyClass 생성자가 던져 넣을 수있는 MyClass의 컨테이너를 생성하는 방법?
#include "MyImage.hpp" // MyImage wraps the Qt library image class
namespace fs = boost::filesystem;
class ImageCollection {
public:
ImageCollection(const char* path);
private:
const fs::path path_;
deque<MyImage> instanceDeque_;
}
ImageCollection(const char* path) :
path_(fs::is_directory(path) ?
fs::complete(path) :
fs::complete(path).parent_path()) /* Can I even do this? */
{
/*** code in question ***/
fs::directory_iterator endIter;
for(fs::directory_iterator dirIter(path_); dirIter != endIter; dirIter++) {
instanceDeque_.push_back(MyImage(*dirIter));
}
}
* dirIter가 비 이미지 파일에 대한 fs :: 경로 일 때 MyImage 생성자가 MyInvalidFileException을 발생시킵니다.
MyImage 및 ImageCollection을 불변으로하고 싶습니다.
수 :
try {
instanceDeque_.push_back(MyImage(*dirIter));
}
catch(const MyInvalidFileException& e) { // oops, tnx Nemanja T.
// remember *dirIter in a list of non-Image files, to use later
continue;
}
그것이 던지면 어떻게됩니까? Deque에 좀비 MyImage 또는 좀비 요소가 남아 있습니까? 아니면 실제로 이것이 올바른 방법일까요? (즉, push_back()이 중단되고 MyImage가 생성되지 않습니다.)
현재 지저분한 해결 방법이 있습니다.
// load up an empty MyImage, which I'd rather not do
instanceDeque_.push_back(MyImage());
for(fs::directory_iterator dirIter(path_); dirIter != endIter; dirIter++) {
MyImage& attemptImage = instanceDeque_.back();
bool success = attemptImage.loadPath(*dirIter); // "fill" the empty MyImage
if (success)
instanceDeque_.push_back(MyImage()); // prepare another empty MyImage
}
instanceDeque_.pop_back(); // discard the empty MyImage
여기서 MyImage는 null QImage *로 초기화되고 loadPath()는 QImage 힙에. 이로 인해 모든 곳에서 널 포인터 검사가 발생합니다. 나는 파일을 열 수 있다면 QImage의 인스턴스를 가질 수있는 방법이 있어야하고, 파일을 열 수 없다면 생성이 실패 할 것이라고 생각한다.
값이 아닌 const 참조를 사용하여 캐치합니다. –
QDir과 친구들이 바로 거기에있을 때 Boost를 사용하여 경로를 관리하는 이유는 무엇입니까? 또한 deque는 쓸모가 없습니다. QList는 많은 경우에 매우 훌륭하게 수행되며 더 깨끗하고 사용하기 쉬운 API를 제공합니다. – CMircea
QList 인터페이스는 어떻게 더 좋습니까? 그것은 나에게 똑같이 보입니다. – Skurmedel