简体   繁体   English

C ++中字符串和ofstream参数的类Java null

[英]Java-like null for string and ofstream arguments in c++

I have some Java code (constructor) 我有一些Java代码(构造函数)

RecursiveDescentParser
(
  std::string inputStream, 
  bool fileService, 
  std::string filePathName, 
  std::ofstream writer
)
{
  input_ = inputStream;
  tokenizer_ = new Tokenizer(inputStream);
  if (fileService == true){
      error = new ErrorHandling(fileService, std::move(writer));
  }
  else{
      error = new ErrorHandling(fileService, std::ofstream());
  }     
  compiled_ = "";
}

Tokenizer *tokenizer_;

std::string input_, compiled_;

I would like to emulate a call within c++ 我想在C ++中模拟通话

RecursiveDescentParser *parser = new RecursiveDescentParser
(
  stream, 
  false, 
  null, 
  null
);

If I use pointer arguments 如果我使用指针参数

std::string *str; std::ofstream *out

I can pass in nullptr but if I choose not to use pointer arguments, I can't pass null. 我可以传递nullptr,但是如果我选择不使用指针参数,则不能传递null。 What can I do to simulate passing null to a std::string and std::ofstream? 如何模拟将null传递给std :: string和std :: ofstream?

Pointers are one option as you say; 正如您所说的那样,指针是一种选择。 but create complications as you need to either manage the objects separately, or use smart pointers. 但由于您需要分别管理对象或使用智能指针而造成麻烦。 Shared pointers are a possibility, and give semantics quite similar to Java's object references (only with reference counting rather than garbage collection). 共享指针是一种可能,它的语义与Java对象引用非常相似(仅使用引用计数而不是垃圾回收)。

For a nullable object type, you could use Boost.Optional , or implement your own nullable wrapper class. 对于可为空的对象类型,可以使用Boost.Optional ,或实现自己的可为空的包装器类。 There is talk of optional being included in a future standard library; 据说将来的标准库中将包含optional but for now, you need Boost for that. 但就目前而言,您需要使用Boost。

Alternatively, it might make more sense to provide a second constructor which does not take those arguments at all. 或者,提供第二个完全不接受这些参数的构造函数可能更有意义。

This is how I would change your usage: 这就是我将如何更改您的用法:

RecursiveDescentParser
(
  std::string inputStream, 
  bool fileService = false, 
  std::string filePathName = "", 
  std::ofstream* writer = NULL
)

RecursiveDescentParser *parser = new RecursiveDescentParser(stream);

You have two options - either create a wrapper class that represents an optional value or use pointers. 您有两个选择-创建代表可选值的包装器类或使用指针。 I strongly recommend the first option. 我强烈建议第一种选择。

Also in some cases you may considered an empty string to be equivalent to not set value. 同样在某些情况下,您可能会认为空字符串等效于未设置的值。

sadly, strings in C++ are objects, and unlike java, C++ objects cannot be null. 可悲的是,C ++中的字符串是对象,并且与Java不同,C ++对象不能为null。

From what I understand, you are creating a constructor to a RecursiveDescentParser. 据我了解,您正在创建RecursiveDescentParser的构造函数。 The constructor's job is to initialize the object. 构造函数的工作是初始化对象。

If the constructor was allowed to accept a pointer, it could accept a null path string, then it will have been created with a null path string, because it is constructor's job is to initialize all fields, and null fields are not initialized, this would not be a good solution. 如果允许构造函数接受指针,则它可以接受空路径字符串,那么它将使用空路径字符串创建,因为构造函数的工作是初始化所有字段,并且不初始化空字段,因此并不是一个好的解决方案。

My suggestion is to make the constructor private, but create a public method that can accept the parameters the constructor would, but also a pointer to the string rather than the object. 我的建议是将构造函数设为私有,但创建一个公共方法,该方法可以接受构造函数将要使用的参数,但也可以是指向字符串而不是对象的指针。 If the path string pointer is null, throw an exception, or return an error object. 如果路径字符串指针为null,则引发异常或返回错误对象。 If the path string is valid, create a new parser from reference to the string pointed to by path string pointer, and return this reference. 如果路径字符串有效,请从对路径字符串指针指向的字符串的引用创建一个新的解析器,然后返回该引用。

Although, I could be totally off, but this is just a suggestion, and I welcome all constructive criticisms on why this would not work. 虽然,我可能会完全不满意,但这只是一个建议,我欢迎所有关于为何无效的建设性批评。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM