Jump to content
  • Advertisement
Sign in to follow this  
Wizzard033

Const Correctness A Reference to a Pointer to a Const Char

This topic is 3808 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

To my understanding, we use references to prevent local copies of data from being created by functions that take parameters. Then, assuming we don't want to modify the data passed to our functions, we ensure the data stays consistent by declaring the const keyword. I guess this makes our programs use a lot less memory. Now, what if we had a pointer to a const char. How would I create a reference to a pointer to a const char? Like this? void foo(const char *const &foo) The goal of the above code is to make it so we can use the function like so: foo("foo"); It does work, but my question is, is it working how I want it to and is there any other ways to write it?

Share this post


Link to post
Share on other sites
Advertisement
There is no advantage of passing a const char* const & to a function, as opposed to passing a simple const char*. The only copy you avoid, in the first case, is that of the pointer itself (but the compiler instead generates machine code to copy the reference used to reference the pointer, which gains you nothing and results in an additional dereference—assuming that the optimizer doesn't reduce the first method to the second one automatically).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!