我创建了一个DirList类来获取特定目录中的所有目录,文件列表。下面是DirList代码。
getDerivedStateFromProps(nextProps, prevState) {
console.log("Hello is anyone there?");
console.log(nextProps);
console.log(prevState);
//Never called - supposed to update when new props received
}
const mapStateToProps = state => {
const { ha, foo, bar } = state.groove;
const { user, authorization } = state.auth;
return {
ha,
foo,
bar,
user,
authorization, //this is the ever so special token i need
};
};
export default connect(mapStateToProps, { unrelatedFunctionName })(ComponentIWantToReRender);
DirList类使用如下
class DirList {
private:
std::string dir_path_;
int type_;
DIR *dir_;
public:
DirList(std::string dir_path);
DirList(std::string dir_path, int type);
~DirList();
std::string GetNext();
};
DirList::DirList(std::string dir_path) {
DirList(dir_path, 0);
}
DirList::DirList(std::string dir_path, int type) {
dir_path_ = dir_path;
type_ = type;
dir_ = opendir(dir_path_.c_str());
if (dir_ == NULL)
std::cout << "DirList - opendir error - " << dir_path_;
Logger(Logger::kError) << "path " << dir_path_ << " type " << type_;
Logger(Logger::kError) << "address " << (uint64_t)this;
}
DirList::~DirList() {
if (closedir(dir_) != 0)
std::cout << "DirList - close error - " << dir_path_;
}
std::string DirList::GetNext() {
Logger(Logger::kError) << "path " << dir_path_ << " type " << type_;
Logger(Logger::kError) << "address " << (uint64_t)this;
if (dir_ == NULL) {
std::cout << "DirList - opendir error - " << dir_path_;
return "";
}
dirent *dirent;
if(type_ == 0){
while ((dirent = readdir(dir_)) != NULL) {
// Jump current and upper dir
if (dirent->d_name[0] == '.')
continue;
else
break;
}
}
else{
while ((dirent = readdir(dir_)) != NULL) {
// Jump current and upper dir
if ((dirent->d_name[0] == '.') || (dirent->d_type != type_))
continue;
else
break;
}
}
if (dirent == NULL)
return "";
return dirent->d_name;
}
当构造函数中的此地址和GetNext方法中的此地址时,DirList在某些代码中运行良好。但是当这个地址不同时,DirList并没有在某些代码中运行...所以dir_path_,type_有错误的值。
我不明白为什么实例的地址被改变了。我没有解决操作只使用new来分配内存。
My Devel Env是Ubuntu 17.10 64bit and clang - ++ 3.8。但我尝试了Ubuntu 16.04 64bit,clang - ++ 4.0。 clang - ++ 5.0 ......但同样的问题也出现了。
感谢
答案 0 :(得分:0)
我找到了原因
DirList::DirList(std::string dir_path) { DirList(dir_path, 0); }
&#39; DirList(dir_path,0)&#39;不是init这个实例。它只会使临时实例。
解决这个问题
DirList::DirList(std::string dir_path) : DirList(dir_path, 0) {}