Calling const-members

E

et al.

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!
 
S

Sensei

On Sep 6, 1:27 pm, et al. <[email protected]

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.
 
J

Juha Nieminen

gwowen said:
On Sep 6, 1:27 pm, et al. <[email protected]

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();
}
//-------------------------------------------------------------------
 
B

Bart van Ingen Schenau

//-------------------------------------------------------------------
#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 said:
    }

};

Bart v Ingen Schenau
 
V

Vladimir Jovic

Bart said:
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
 
A

Alf P. Steinbach /Usenet

* Bart van Ingen Schenau, on 08.09.2010 09:22:
When only changing the const-ness, I would advocate using a
const_cast:

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
 
R

Richard

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

"Alf P. Steinbach /Usenet" <[email protected]> spake the secret code
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?
 
A

Alf P. Steinbach /Usenet

* Richard, on 08.09.2010 18:26:
[Please do not mail me a copy of your followup]

"Alf P. Steinbach /Usenet"<[email protected]> spake the secret code
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top