c++ - const qualifier disappears from pure virtual function -



c++ - const qualifier disappears from pure virtual function -

this question has reply here:

top-level const doesn't influence function signature 7 answers

using gcc version 4.8.2:

i'm running issue const qualifier on parameters disappearing when compile code. here example:

main.cc:

#include <iostream> class base of operations { public: virtual int getsum( const int number ) = 0; }; class derived : public base of operations { public: derived( const int& num ) : _mynumber( num ) {} virtual int getsum( const int number ) { homecoming _mynumber + number; } private: int _mynumber; }; int main( int argc, const char* argv[] ) { base of operations *b = new derived( 2 ); std::cout << b->getsum( 3 ) << "\n"; }

compiled so:

g++ main.cc -o const_test

when run nm:

nm const_test | c++filt | grep getsum

i next output:

0000000000400b60 w derived::getsum(int)

why const disappear function when compiles?

your function signature

virtual int getsum(const int number) = 0;

is equivalent

virtual int getsum(int number) = 0;

const has no effect on function signature declaration parameters passed value.

the effect is, can't alter parameter instance on stack within of potential definition of method. it's in fact sufficient set there, prevent changing parameter's instance in function body.

c++ const pure-virtual

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -