1

I'm creating a string parser class and there's lots of sub-sub private member functions that all need access to an input. I want to avoid having to put the input as a parameter of every function e.g.

string out = func(input){ sub_func(input) { sub_sub_func(input) } } }

I keep hearing raw pointers are bad, but still not getting a clear answer to this specific situation of "simply using a pointer to refer to something". I could do

string m_str //declared as private member
func(string& input) { m_str = move(input) } //member function

or

string& m_str; //declared as a private member
myclass (string input) : m_str(input); //class construction

but what I want to do is

string m_str* //declared as a private member
func(string input) { m_str = input } //member function

QUESTION

  1. Do I need to set m_str to nullptr before myclass object goes out of scope (i.e. put this into the class destructor)?
  2. Should I use unique instead of raw pointer?
Elan Hickler
  • 1,099
  • 1
  • 11
  • 28
  • 1
    I don't say raw pointers are "bad", but it is easy to do bad things with them. If you don't need to transfer ownership and your objects won't outlive the data, raw pointers shouldn't pose any problems. – Weak to Enuma Elish Nov 26 '15 at 15:43
  • you could consider [`observer_ptr`](http://en.cppreference.com/w/cpp/experimental/observer_ptr) – sp2danny Nov 26 '15 at 15:57
  • @sp2danny thanks, more info on observer_ptr: http://stackoverflow.com/questions/24157735/is-there-an-implementation-for-observer-ptr-now – Elan Hickler Nov 26 '15 at 17:32
  • @sp2danny what am I missing? I don't see any advantage to `observer_ptr` over a raw pointer. – Mark Ransom Nov 27 '15 at 04:12
  • it declares intent. raw pointers are ok, if only used in an observer role. – sp2danny Nov 27 '15 at 08:45

3 Answers3

3

For your first question, no you don't need to "reset" any member variables. The object is destructed and should not be used again.

For the second question, it depends. Most of the time you can look at the new smart pointers not as pointers, but from a resource ownership perspective: Can a resource have multiple simultaneous owners (std::shared_ptr), or only one owner at a time (std::unique_ptr)? If you are not going to transfer ownership then there is no need to use a smart pointer really, except as a nice auto-deleting pointer. The bigger question you should ask yourself is, do you need to use pointers? Very often the answer to that is "no".

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1
  1. No, you don't need to assign a pointer type to nullptr on destruction.

  2. Your "or" case will give you a dangling reference once input goes out of scope. That's undefined behaviour.

  3. As for using std::unique_ptr, decide on a case by case basis. std::shared_ptr might be a better choice if you want to have more than one thing "owning" the pointer.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

Since you are trying to parse a std::string, have you considered to pass iterators to your functions? Those could simply point to the space in the string, your parser is currently reading.

BTW, I'd not be happy with the design of functions in functions. Consider to create a class with these functions as private member functions. Then you can have the string as class member and every function can simply use the same string instance.

cdonat
  • 2,748
  • 16
  • 24
  • 1
    I am creating a class with private member functions, I will improve my question so that is clear. – Elan Hickler Nov 26 '15 at 15:50
  • iterator is a good idea! Is there a shorthand to get a string from beginning to end from an iterator? for example I need to do stuff like `"error, here is your string: " + input` – Elan Hickler Nov 26 '15 at 16:39
  • `std::begin(mystring)` and `std::end(mystring)`: initialize a copy of the string with these begin and end iterators: `std:.string{begin, end}`. – cdonat Nov 26 '15 at 16:40