passing a pointer by reference in c++

后端 未结 2 1443
说谎
说谎 2021-01-12 23:54

I have a function where I\'m passing in an iterator to a char * buffer (which is also a char *). The function needs to increment the iterator. Anyway, I found that a good

2条回答
  •  鱼传尺愫
    2021-01-13 00:15

    Is there a compiler difference between these two methods (I'm using Visual Studio 2005)?

    As others have correctly noted, "no".

    Which is correct?

    Between the two alternatives, it entirely comes down to the "should I hide pointers behind typedefs" debate. There are valid arguments for either position

    However, I think that both of your code snippets suffer from over-specialization. I prefer to code algorithms as template functions so that I Don't Repeat Myself.

    If your design supports it, you could generalize your code to accept any iterator:

    template 
    bool myFunction(InputIterator &iter)
    {
      std::cout << *iter;
      ++iter;
      return true;
    }
    

提交回复
热议问题