c++ - Calling a const function rather than its non-const version -


I tried to wrap something like Qt's Shared Data Points for my purposes, and on the test I came to know that when const Function should be called, its non-current version was chosen instead.

I am compiling with the C + + 0x option, and here is a minimum code:

  struct data {int x (const const}; }}; Template & lt; Class T & gt; Straight container {container () {ptr = new T (); } T & amp; Operator * () {puts ("non-contact data PTR"); Return * PTR; } T * operator-> () {Puts ("non-contact data PTR"); Return PTR; } Const T & amp; Operator * () const {puts ("const data ptr"); Return * PTR; } Const T * operator- & gt; () Const {puts ("const data ptr"); Return PTR; } T * PTR; }; Typeffe container & lt; Data & gt; TestType; Zero test () {testType test; Test & gt; X (); }   

As you can see, Data.x is a constant function, so operator - & gt; The const is supposed to be one and when I comment about non-constant, it compiles without errors, so this is possible. Still my terminal is printed:

"non-contact data ptr"

Is this a GCC bug (I have 4.5.2), Or is there something I'm missing?

If you have two overloads that differ only in their const -ness , The compiler fixes the call on this basis whether * is is const or not in your example code, test is not < Code> const , therefore non- const is called overload.

If you have this:

  test type test; Constant type type & amp; Test2 = test; Test2- & gt; X ();   

You should see that other surcharges are called, because test2 is const .

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -