Syntax C ++ Pass by const

I know that the topic of following a link versus following a pointer is heavily covered ... I pretty much understood all the nuances until I read this:

http://carlo17.home.xs4all.nl/cpp/const.qualifier.html

which reads (in case the link doesn't work)

The prototype for foobar can have any of the following footprints: void foobar(TYPE); // Pass by value void foobar(TYPE&); // Pass by reference void foobar(TYPE const&); // Pass by const reference Note that I put the const to the right of TYPE because we don't know if TYPE (this is not a template parameter, but rather for instance a literal char*) is a pointer or not! 

which means the author: "Please note that I put const to the right of TYPE, because we don’t know if TYPE ... is a pointer or not!"

Everything I read on this topic was consistent:

void foodbar (TYPE const &)

also equivalent

void foobar (const TYPE &)

If I understand the author correctly, he / she says that:

const int * X vs int * const X, where is the pointer, is X itself a constant, against which X points to const?

If so, is that true?

+7
source share
3 answers

If TYPE is #define for something like int* , the placement of const matters. In this case, you get const int* or int* const depending on the placement of const .

If TYPE is a typedef or template parameter, const will affect the whole type anyway.

For me, it looks more like another argument against macros, rather than the need for some specific style in ads.

+9
source

Considering the C ++ FAQ Lite (as follows from the article), you read pointer declarations from right to left. Therefore, if TYPE is a pointer, placing * makes a difference. Follow the link for the full story.

+1
source
 void foobar(TYPE const&); // Pass by const reference 

If TYPE is a pointer to type ABC, then

 void foobar(ABC* const&) 

differs from

 void foobar(const ABC* &) 

I believe that everything the author strives for is

EDIT

This also applies if typedef is a pointer.

 typedef SomeStruct* pSomeStruct; void foobar(pSomeStruct* const &); // const reference to a pointer // to a pointer to SomeStruct void foobar(const pSomeStruct* &); // reference to a pointer // to a const pointer to const SomeStruct 
0
source

All Articles