0

I have some Java code (constructor)

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++

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. What can I do to simulate passing null to a std::string and std::ofstream?

16
  • Why not use default arguments and default initialize them? Commented Apr 5, 2013 at 13:37
  • you can use boost::optional or make another mechanism to tell your function that the parameter is not passed. Commented Apr 5, 2013 at 13:37
  • @jotep Default initialize them to what? Commented Apr 5, 2013 at 13:38
  • @MikeSeymour Empty string and stdout Commented Apr 5, 2013 at 13:39
  • @andre please read the question more carefully. OP says if I use pointers I can pass NULL but if I don't what can I do? Commented Apr 5, 2013 at 13:40

4 Answers 4

2

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).

For a nullable object type, you could use Boost.Optional, or implement your own nullable wrapper class. There is talk of optional being included in a future standard library; but for now, you need Boost for that.

Alternatively, it might make more sense to provide a second constructor which does not take those arguments at all.

Sign up to request clarification or add additional context in comments.

6 Comments

I read Boost.Optional and would like a clear concrete example of how it would be implemented for parameters.
@Mushy: The parameter would be boost::optional<std::string>, and you could pass either a string or boost::none.
What is the #include<> path for Boost.Optional?
@Mushy: boost/optional.hpp
I having a problem with boost::optional<std::ofstream> related to no copy/no assign. If I specify, for example, const boost::optional<std::ofstream> &writer as an argument, how am I to do this?
|
1

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);

Comments

0

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.

Comments

0

sadly, strings in C++ are objects, and unlike java, C++ objects cannot be null.

From what I understand, you are creating a constructor to a 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. 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.

5 Comments

C++ strings are no more references than ints are references. C++ std::string are full on value types.
Strings are not references, they're objects. But you're correct that objects can't be null.
Modified it. No need to downvote, I am willing to correct all inaccuracies, I am merely suggesting a solution.
You might want to change or remove the paragraph about "null fields" too, since that makes no sense at all.
I modified it to make more sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.