传递 char ** 作为引用并返回 const char * 作为引用
我有一个类,它解析命令行参数,然后将解析后的值返回给客户端类。为了进行解析,我需要将 argv 传递给解析函数。我想通过引用传递,但据我所知,我们从不使用“&”传递数组时的符号。数组不是可以通过引用传递的对象。这是我的代码:
#include <iostream>
#include <fstream>
using namespace std;
class cmdline
{
const char * ifile;
public:
cmdline():ifile(NULL){}
const char * const getFile() const
{
return (ifile);
}
void parse(int argc,const char** argv)
{
//parse and assign value to ifile
// ifile = optarg;
// optarg is value got from long_getopt
}
};
int main(int argc, char ** argv)
{
cmdline CmdLineObj;
CmdLineObj.parse(argc, const_cast<const char**>(argv));
const char * const ifile = CmdLineObj.getFile();
ifstream myfile (ifile);
return 0;
}
1)处理 argv
的方式正确吗?
2)更好的处理方式,ifile
?
3)我想返回ifile
作为参考,如果需要的话我应该做什么改变?
我的代码按照它应该的方式工作,但我这样做的原因是“不仅仅是让它工作”,而是正确地执行它。
感谢您的帮助。
编辑:: 在 Mehrdad 发表评论后,我进行了这样的编辑:
class CmdLine
{
const char * ifile;
public:
const char * & getFile() const
{
return (ifile);
}
但我收到错误 - 'const char*&' 类型的引用初始化无效来自“const char”类型的表达式
I have a class which parses the Command line arguments and then returns the parsed value to the client class. For parsing, I need to pass argv
to parse function. I would like to pass by reference but from what I know , we never use the '&' symbol when passing arrays. Arrays are not objects that can be passed by reference. Here is my code:
#include <iostream>
#include <fstream>
using namespace std;
class cmdline
{
const char * ifile;
public:
cmdline():ifile(NULL){}
const char * const getFile() const
{
return (ifile);
}
void parse(int argc,const char** argv)
{
//parse and assign value to ifile
// ifile = optarg;
// optarg is value got from long_getopt
}
};
int main(int argc, char ** argv)
{
cmdline CmdLineObj;
CmdLineObj.parse(argc, const_cast<const char**>(argv));
const char * const ifile = CmdLineObj.getFile();
ifstream myfile (ifile);
return 0;
}
1) Is the way argv
is treated, correct?
2) Better way to handle, ifile
?
3) I want to return ifile
as reference, what change should I do, if needed?
My code works the way it is supposed to work, but the reason I came to SO is to "not-just-make-it-work" but do it properly.
Thanks for your help.
EDIT:: After Mehrdad's comment, I edited like this:
class CmdLine
{
const char * ifile;
public:
const char * & getFile() const
{
return (ifile);
}
But I get the error - invalid initialization of reference of type ‘const char*&’ from expression of type ‘const char’
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是什么让你这么想?
你为什么要 const 铸造它?您可以将
main
的定义更改为const char** argv
,而不是强制转换。好吧,总是有
std::string
,但由于您似乎所做的就是将值传递给std::ifstream
我没有看到任何意义使用它。返回指针作为引用有什么意义?您是否期望 getFile 的调用者实际更改指向此类字符串的成员?您不应该这样做,因为
getFile
是一个const
成员函数。如果您考虑性能,那么在这种情况下返回对指针的引用实际上会比按值返回指针更糟糕。从getFile
返回时,字符串内容不会被复制,就像ifile
是std::string
时那样(在这种情况下返回const 引用是有意义的)。What makes you think that?
Why are you const casting that? Instead of casting, you could change your definition of
main
toconst char** argv
.Well, there is always
std::string
, but since all you seem to do is then pass the value to anstd::ifstream
I don't see a point in using it.What would be the point of returning a pointer as a reference? Are you expecting callers of
getFile
to actually change the member that points to such string? You shouldn't be doing that sincegetFile
is aconst
member function. If you are thinking performance, then returning a reference to a pointer in this case will actually be worse than returning the pointer by value. The string contents are not getting copied when returned fromgetFile
, like they would ififile
was instead anstd::string
(in which case returning a const reference would make sense).