1

I just wondered what's the difference between having a constructor with const char* c or char* const c as a parameter? If I swap char* and const in header and source file it isn't a problem but when I mix them it won't compile, saying there is no such overloaded constructor.

When I use my own class for a copy constructor the compiler doesn't seem to bother if I have MyClass const &other and const MyClass &other mixed..

Can anyone enlighten me please? :)

2
  • 1
    The problem is that you're not using std::string. Commented Aug 3, 2014 at 18:50
  • 1
    If you're going to post code, you should at least post code that illustrates the question you're trying to ask. Commented Aug 3, 2014 at 18:53

4 Answers 4

3

This record const char *c declares pointer c that points to an object of type const char. That is using this pointer you may not change the object that refered to by the pointer.

This record char * const c declares const pointer c that points to a non-const object of type char. You may not change the pointer itself but you may change the object refered to by the pointer.

There is a difference between declaring pointers and references. Though there is used term "constant reference" strictly speaking references themselves may not be constant. According to the C++ grammar

ptr-operator:
* attribute-specifier-seqopt cv-qualifier-seq
& attribute-specifier-seqopt

references do not contain cv-qualifier-seq. So for example this record

Airplane & const other

will be incorrect and the compiler shall issue an error. As for these records

const Airplane & other
Airplane const & other

then they are equivalent the same way as equivalent the recirds below

const char *c
char const *c
Sign up to request clarification or add additional context in comments.

1 Comment

Ah thanks, couldn't remember that! Visual Studio compiles Airplane const &other without a word..
2

Think of the * as a "fence". What's before it in the declaration defines the type that the pointer refers to. What's after the * defines the pointer itself. For this case, it's easiest to read things backwards (translating * as "pointer to"), so char const *t is read (from back to front) as: "t is a pointer to a const char". Likewise, char *const t is read as: "t is a const pointer to a char."

  1. char *const means the pointer itself is const (can't be modified), but the char(s) it points at can be modified.
  2. char const * or const char * are equivalent to each other. Both mean the pointer itself can be modified, but what it points at cannot.

Substituting a different type for char doesn't change that basic idea.

The same is true with a reference: const T & means a reference to a const T, so you can't modify the T that it refers to. In the case of a reference, you can't modify the reference itself to refer to a different object, so T & const doesn't make any real sense (and isn't allowed).

Comments

0

const applies to the type to its left (or to its right if there is nothing on its left), so const char * and char const * are the same and mean that the characters cannot change, but the pointer can, whereas char * const means that the characters can change but the pointer cannot

Comments

0

The order in which const and char* are written actually make a difference. If you have const char* in your header but char* const in your source file, you will get an error because the compiler won't regard these as the same.

const char* p means that p points to a char that is constant. We can alter where p points to, but we can never alter the contents stored at p.

char* const p means that p is a constant pointer and is pointing to a char. We can alter the contents stored at p, but we cannot alter where p points to.

const char* const p means that p is a constant pointer to a constant char. We cannot alter where p points to nor can we alter the contents at p. Hope this is helpful!

Comments

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.