c++ - How to call a non-const overload? -


what's difference betweeen void func(const int& i) , void func(int& i). if const cut off @ top level, possible call second overload? why const overloads preferred? following select first overload:

func(42); func(int{42}); int = 42; func(i); int &j = i; func(j); func(i + 1); 

whoops, see problem now. had typed cout << "const\n" in both functions, looked calling first overload. sorry guys.

const hint yourself , other developers, don't intend modify observed object. const overload selected if argument const:

#include <iostream>  void f(const int&) {     std::cout << "f(const int&)\n"; }  void f(int&) {     std::cout << "f(int&)\n"; }  int main() {     int = 0;     const int b = 0;     int& c = a;     const int& d = a;     f(a);     f(b);     f(c);     f(d); } 

this output

f(int&) f(const int&) f(int&) f(const int&) 

see this demo.

as can see, not const overload.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -