Calling const-members

  • Follow


Hi again! I am still learning through examples, and still I am 
struggling with matrix classes! :)

I have a probably naive question: how do I "force" C++ to use a const 
member? I mean, I have learned (through your suggestions) about const 
members, for example my at() member in the matrix class:

class matrix
{
public:
    matrix(unsigned int rows, unsigned int cols);
    matrix(matrix const& src);

    virtual ~matrix();

    virtual double& at(unsigned int r, unsigned int c);
    virtual double  at(unsigned int r, unsigned int c) const;
    // ...
}


Now, when I print my matrix using std::cout, the non-const member is 
called, while I was expecting otherwise! My logging member is nothing 
but a simple double loop:


void matrix::log()
{
    // ...
    for (i = 0; i < getRows(); i++)
    {
        for (j = 0; j < getColumns(); j++)
            cout << showpos << scientific << at(i, j) << "   ";

        cout << endl;
    }
}


What am I missing here?

Thanks!

0
Reply et 9/6/2010 12:27:15 PM

On Sep 6, 1:27=A0pm, et al. <et...@google.m

If your log() function is not const, it will not call the const
members. Try:

void matrix::log() const;
0
Reply gwowen 9/6/2010 12:39:56 PM


On 2010-09-06 14:39:56 +0200, gwowen <gwowen@gmail.com> said:

> On Sep 6, 1:27�pm, et al. <et...@google.m
> 
> If your log() function is not const, it will not call the const
> members. Try:
> 
> void matrix::log() const;


You're totally right, that works!


Thanks!


-- 

Sensei�<Sensei's e-mail is at Me-dot-com>

Research (n.): a discovery already published by a chinese guy one month
� � � � � � �� before you, copying a russian who did it in the 60s.


0
Reply sensei.wa (15) 9/6/2010 1:00:28 PM

gwowen <gwowen@gmail.com> wrote:
> On Sep 6, 1:27�pm, et al. <et...@google.m
> 
> If your log() function is not const, it will not call the const
> members. Try:
> 
> void matrix::log() const;

  If for whatever reason one would want to call the const version of
a method from a non-const method (which cannot be made one), one could
always cast 'this' to a const-pointer and call the method through that.

  I don't know if there's a "better" way of doing it other than that.

//-------------------------------------------------------------------
#include <iostream>

class A
{
 public:
    void function() { std::cout << "non-const function()\n"; }
    void function() const { std::cout << "const function()\n"; }

    void functionWhichMustBeNonConst()
    {
        function(); // call the non-const version
        static_cast<const A*>(this)->function(); // call the const version
    }
};

int main()
{
    A a;
    a.functionWhichMustBeNonConst();
}
//-------------------------------------------------------------------
0
Reply Juha 9/7/2010 7:27:14 PM

On Sep 7, 9:27=A0pm, Juha Nieminen <nos...@thanks.invalid> wrote:
> //-------------------------------------------------------------------
> #include <iostream>
>
> class A
> {
> =A0public:
> =A0 =A0 void function() { std::cout << "non-const function()\n"; }
> =A0 =A0 void function() const { std::cout << "const function()\n"; }
>
> =A0 =A0 void functionWhichMustBeNonConst()
> =A0 =A0 {
> =A0 =A0 =A0 =A0 function(); // call the non-const version
> =A0 =A0 =A0 =A0 static_cast<const A*>(this)->function(); // call the cons=
t version

When only changing the const-ness, I would advocate using a
const_cast:
  const_cast<const A*>(this)->function(); // call the const version

> =A0 =A0 }
>
> };
>

Bart v Ingen Schenau
0
Reply bart855 (270) 9/8/2010 7:22:46 AM

Bart van Ingen Schenau wrote:
> On Sep 7, 9:27 pm, Juha Nieminen <nos...@thanks.invalid> wrote:
>> //-------------------------------------------------------------------
>> #include <iostream>
>>
>> class A
>> {
>>  public:
>>     void function() { std::cout << "non-const function()\n"; }
>>     void function() const { std::cout << "const function()\n"; }
>>
>>     void functionWhichMustBeNonConst()
>>     {
>>         function(); // call the non-const version
>>         static_cast<const A*>(this)->function(); // call the const version
> 
> When only changing the const-ness, I would advocate using a
> const_cast:
>   const_cast<const A*>(this)->function(); // call the const version

I thought that is an indication of broken design
0
Reply Vladimir 9/8/2010 9:33:43 AM

* Bart van Ingen Schenau, on 08.09.2010 09:22:
> On Sep 7, 9:27 pm, Juha Nieminen<nos...@thanks.invalid>  wrote:
>> //-------------------------------------------------------------------
>> #include<iostream>
>>
>> class A
>> {
>>   public:
>>      void function() { std::cout<<  "non-const function()\n"; }
>>      void function() const { std::cout<<  "const function()\n"; }
>>
>>      void functionWhichMustBeNonConst()
>>      {
>>          function(); // call the non-const version
>>          static_cast<const A*>(this)->function(); // call the const version
>
> When only changing the const-ness, I would advocate using a
> const_cast:
>    const_cast<const A*>(this)->function(); // call the const version
>
>>      }
>>
>> };

Someone once advocated not doing that, because it generates false positives when 
searching for bad casts. Instead, that person argued, one should simply declare 
a reference to self. Like

    A const& constSelf = *this;
    constSelf.function();


Cheers,

- Alf

-- 
blog at <url: http://alfps.wordpress.com>
0
Reply Alf 9/8/2010 1:15:55 PM

[Please do not mail me a copy of your followup]

"Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> spake the secret code
<i682ag$4f7$1@news.eternal-september.org> thusly:

>> When only changing the const-ness, I would advocate using a
>> const_cast:
>>    const_cast<const A*>(this)->function(); // call the const version
>>
>>>      }
>>>
>>> };
>
>Someone once advocated not doing that, because it generates false
>positives when 
>searching for bad casts.

The assumption being that any const_cast<> is a "bad" cast?
-- 
"The Direct3D Graphics Pipeline" -- DirectX 9 draft available for download
 <http://legalizeadulthood.wordpress.com/the-direct3d-graphics-pipeline/>

      Legalize Adulthood! <http://legalizeadulthood.wordpress.com>
0
Reply jeeves (198) 9/8/2010 4:26:39 PM

* Richard, on 08.09.2010 18:26:
> [Please do not mail me a copy of your followup]
>
> "Alf P. Steinbach /Usenet"<alf.p.steinbach+usenet@gmail.com>  spake the secret code
> <i682ag$4f7$1@news.eternal-september.org>  thusly:
>
>>> When only changing the const-ness, I would advocate using a
>>> const_cast:
>>>     const_cast<const A*>(this)->function(); // call the const version
>>>
>>>>       }
>>>>
>>>> };
>>
>> Someone once advocated not doing that, because it generates false
>> positives when
>> searching for bad casts.
>
> The assumption being that any const_cast<>  is a "bad" cast?

Presumably the assumption is that any const_cast that casts away const may be a 
bad cast, and that grepping for const_cast is easy while grepping for const_cast 
that casts away const is not practically doable.

However, thinking about it there is also a readability argument for not using 
const_cast unnecessarily.

Because the cast notation communicates that something is being forced, while 
adding constness is not something that needs to be forced.


Cheers,

- Alf

-- 
blog at <url: http://alfps.wordpress.com>
0
Reply Alf 9/8/2010 4:34:13 PM

8 Replies
128 Views

(page loaded in 0.109 seconds)

Similiar Articles:













7/15/2012 4:08:47 AM


Reply: