-1

I'm new to using C++ to write enterprise software at my company and the usage of const in member functions has been quite confusing to me. For example, I have the following method:

string DAC::impulse_find(int i)

There will be no modification of the variable i. My code compiles perfectly when I use these three variants of adding const to the above method:

const string DAC::impulse_find(const int i) const

const string const DAC::impulse_find(const int i) const

string const DAC::impulse_find(const int i) const

So what exactly is the difference between these three? I looked at some past answers and say "It applies to whatever is to the left" but that is still confusing. Does it apply to anything, such as the name of the class as well?

  • 1
    Returning a constant variable, that is not a reference, is worthless. There is no need for `const` as the function returns a copy. – Thomas Matthews Jul 06 '15 at 17:52
  • http://stackoverflow.com/questions/5598703/c-const-usage-explanation – deviantfan Jul 06 '15 at 17:53
  • 1
    You may use `const string& DAC::impulse_find(int i) const`, instead –  Jul 06 '15 at 17:54
  • 4
    It seems unlikely that you're writing "enterprise software in C++" when you don't know the lexical rules for `const`... Also, programming by guessing does not work. Which book are you using to learn C++? – Lightness Races in Orbit Jul 06 '15 at 17:54
  • @LightnessRacesinOrbit I'm a college student working as a summer intern, so I'm learning C++ as I work on my project. My bad for trying to learn the rules of C++ so I can be a better programmer. – TheSalamander Jul 06 '15 at 17:58
  • 1
    @TheSalamander *"My bad for trying to learn"* that's a rather nonsensical take on what you're reading here. – Drew Dormann Jul 06 '15 at 17:59
  • 2
    @TheSalamander We just don't understand why you're trying to add a const modifier to the return value. The return value is a temporary copy, which means you can't modify it anyway – KABoissonneault Jul 06 '15 at 18:00
  • @TheSalamander: Which book are you using to learn C++? – Lightness Races in Orbit Jul 06 '15 at 18:00
  • @LightnessRacesinOrbit I'm not using any book, I'm just learning as I go by looking at past Stack Overflow threads. So far it has been helpful. I've learned a lot but const has been rather confusing for me. – TheSalamander Jul 06 '15 at 18:02
  • @TheSalamander: You really need to at least skim over a C++ book, even if it is online. At *least* learn the syntax rules and the keywords. – Zan Lynx Jul 06 '15 at 18:05
  • @TheSalamander I'm speculating here, but I bet someone at the place you're interning at would have a book that they would lend you. This site does many things well, but is not a good replacement for an introductory book. – Drew Dormann Jul 06 '15 at 18:06
  • @TheSalamander: You should really get [a book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). As I've said, you cannot program by guesswork. You certainly can't learn by randomly "skimming" random threads on the internet!!! _Study_, like we did in the olden days. Cheers. – Lightness Races in Orbit Jul 06 '15 at 18:08

2 Answers2

3

There is no difference between

const std::string  
std::string const

The order of const is irrelevant since you are not dealing with pointers.

Having many const as a qualifier doesn't make sense. Did you turn up the warning level on your compiler to maximum?

Edit 1: Pointers and Const
Since you are returning a copy of a variable the effectiveness of const is worthless. The original item from the function can't be modified because you are returning a copy.

Maybe you are confused with the pointer syntax, where the placement of const matters:

string const * const -- constant pointer to a constant (read-only string).  Pointer cannot be modified.
string const *       -- Mutable pointer to a constant (read-only) string.  Pointer value can change.
string       * const -- Constant pointer to a read/write string.  Pointer value cannot be changed.
string       *       -- Mutable pointer to a read/write string.  Pointer value can be changed.  

The const for read-only data may be placed before or after the type:

const string *  
string const *

Both of the above are equivalent.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
0

All three version

const string …
const string const …
string const …

are identical.

The placement of const doesn't matter here. The duplicate const in the second version is redundant and your compiler should give a warning about it.

Moreover, the consts don't make sense here because the string is returned as a copy. Returning a const value brings you nothing (see KABoissonneault's comment), only references or pointers to const make sense as a return type.

Community
  • 1
  • 1
Emil Laine
  • 41,598
  • 9
  • 101
  • 157