C++ 我应该如何处理C++;?
比如说,我想创建一个文件类C++ 我应该如何处理C++;?,c++,C++,比如说,我想创建一个文件类 class File{ public: File(const char *file){ openFile(file); } ~File(); isEmpty(); }; openFile检查文件是否存在或文件内容是否有效 File *file = new File("filepath"); if(file) file->isEmpty();
class File{
public:
File(const char *file){
openFile(file);
}
~File();
isEmpty();
};
openFile检查文件是否存在或文件内容是否有效
File *file = new File("filepath");
if(file)
file->isEmpty();
如果我的文件路径是正确的,那么一切都很好,文件实例是正确的,我们可以调用file->isEmpty()代码>
什么是文件不存在,在这种情况下,检查if(file)
的结果仍然为true,并将导致创建实际上无效的文件实例。如何保证如果文件路径无效,文件实例应为null。如果无法打开文件,则构造函数应抛出异常
File::File( const char* pPath )
{
if ( !openFile( pPath ) )
throw FileNotFound( pPath );
}
如果文件无法打开,构造函数应该抛出异常
File::File( const char* pPath )
{
if ( !openFile( pPath ) )
throw FileNotFound( pPath );
}
在构造函数中执行类似检查的唯一方法是抛出异常。但这不是一个好的设计——你应该创建它,检查它是否有效,如果无效则删除它。在构造函数中进行类似检查的唯一方法是抛出一个异常。但这不是一个好的设计——你应该创建它,检查它是否有效,如果无效则删除它。我建议使用静态工厂方法来检查,如果对象无效,则删除它并抛出异常。您将创建如下文件:
File* file = File::open("whatever");
#include <string>
using std::basic_string;
class EmptyFile{};
template<typename T>
class File
{
public:
File(const basic_string<T> &FILE)
{
if (isEmpty(FILE)) throw EmptyFile();
openFile(FILE);
}
bool isEmpty(const basic_string<T> &FILE) const
{ return FILE.empty(); }
};
我建议使用静态工厂方法,为您执行检查,如果对象无效,则删除它并抛出异常。您将创建如下文件:
File* file = File::open("whatever");
#include <string>
using std::basic_string;
class EmptyFile{};
template<typename T>
class File
{
public:
File(const basic_string<T> &FILE)
{
if (isEmpty(FILE)) throw EmptyFile();
openFile(FILE);
}
bool isEmpty(const basic_string<T> &FILE) const
{ return FILE.empty(); }
};
如果我理解正确,当“filepath”
无效时,是否希望从构造函数返回null?在C++中,这不是(直接)可能的,尽管有一些可能性。您可以从构造函数中抛出异常,但在C++中这会变得毛茸茸的。您可以使用一些函数来检查文件对象的有效性,因此if(File)
将变成if(isValid(File))
。您还可以将某些逻辑封装在某种工厂中,如果要创建的文件无效,该工厂将返回null。如果我理解正确,您希望在“filepath”
无效时从构造函数返回null吗?在C++中,这不是(直接)可能的,尽管有一些可能性。您可以从构造函数中抛出异常,但在C++中这会变得毛茸茸的。您可以使用一些函数来检查文件对象的有效性,因此if(File)
将变成if(isValid(File))
。您还可以将某些逻辑封装在某种工厂中,如果要创建的文件无效,该工厂将返回null。我将使用STL、模板并抛出一个空类。现在,您不能从构造函数返回任何内容。。。所以要么这样做:
File* file = File::open("whatever");
#include <string>
using std::basic_string;
class EmptyFile{};
template<typename T>
class File
{
public:
File(const basic_string<T> &FILE)
{
if (isEmpty(FILE)) throw EmptyFile();
openFile(FILE);
}
bool isEmpty(const basic_string<T> &FILE) const
{ return FILE.empty(); }
};
#包括
使用std::basic_字符串;
类清空文件{};
模板
类文件
{
公众:
文件(常量基本字符串和文件)
{
如果(isEmpty(FILE))抛出EmptyFile();
openFile(文件);
}
bool isEmpty(常量基本字符串和文件)常量
{返回文件.empty();}
};
或者你可以这样做:
#include <string>
using std::basic_string;
template<typename T>
class File
{
public:
bool Open(const basic_string<T> &FILE) const
{
bool empty = isEmpty(FILE);
if(!empty)
/* open the file */;
return isEmpty;
}
bool isEmpty(const basic_string<T> &FILE) const
{ return FILE.empty(); }
};
#包括
使用std::basic_字符串;
模板
类文件
{
公众:
布尔打开(常量基本字符串和文件)常量
{
bool empty=isEmpty(文件);
如果(!空)
/*打开文件*/;
返回是空的;
}
bool isEmpty(常量基本字符串和文件)常量
{返回文件.empty();}
};
我会使用STL、模板并抛出一个空类。现在,您不能从构造函数返回任何内容。。。所以要么这样做:
File* file = File::open("whatever");
#include <string>
using std::basic_string;
class EmptyFile{};
template<typename T>
class File
{
public:
File(const basic_string<T> &FILE)
{
if (isEmpty(FILE)) throw EmptyFile();
openFile(FILE);
}
bool isEmpty(const basic_string<T> &FILE) const
{ return FILE.empty(); }
};
#包括
使用std::basic_字符串;
类清空文件{};
模板
类文件
{
公众:
文件(常量基本字符串和文件)
{
如果(isEmpty(FILE))抛出EmptyFile();
openFile(文件);
}
bool isEmpty(常量基本字符串和文件)常量
{返回文件.empty();}
};
或者你可以这样做:
#include <string>
using std::basic_string;
template<typename T>
class File
{
public:
bool Open(const basic_string<T> &FILE) const
{
bool empty = isEmpty(FILE);
if(!empty)
/* open the file */;
return isEmpty;
}
bool isEmpty(const basic_string<T> &FILE) const
{ return FILE.empty(); }
};
#包括
使用std::basic_字符串;
模板
类文件
{
公众:
布尔打开(常量基本字符串和文件)常量
{
bool empty=isEmpty(文件);
如果(!空)
/*打开文件*/;
返回是空的;
}
bool isEmpty(常量基本字符串和文件)常量
{返回文件.empty();}
};
怎么做?你是在建议一种工厂的东西吗?为什么不呢?“C++编码标准”指出,在这些情况下,从构造函数抛出异常是正确的做法。当然,您必须确保所有资源都已正确释放,但就我所知,这与异常的其他用途并无区别。@叔叔,因为错误的文件名不是“异常”。对于通过调用“isValid”方法很容易处理的东西,我不希望产生异常处理的开销。动态分配文件对象可能会有不必要的开销。在任何情况下,你会尝试打开多少文件?我认为“不被认为是好的设计”有点夸大了这一点,但我不认为有人建议你做std::fstream
所做的事情……怎么做?你是在建议一种工厂的东西吗?为什么不呢?“C++编码标准”指出,在这些情况下,从构造函数抛出异常是正确的做法。当然,您必须确保所有资源都已正确释放,但就我所知,这与异常的其他用途并无区别。@叔叔,因为错误的文件名不是“异常”。对于通过调用“isValid”方法很容易处理的东西,我不希望产生异常处理的开销。动态分配文件对象可能会有不必要的开销。在任何情况下,您将尝试打开多少文件?我认为“不被认为是好的设计”稍微夸大了这一点,但我认为不值得因为某人建议您完全按照std::fstream
所做的操作而对其投下否决票。