Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Calling const-members

Reply
Thread Tools

Calling const-members

 
 
et al.
Guest
Posts: n/a
 
      09-06-2010
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!

 
Reply With Quote
 
 
 
 
gwowen
Guest
Posts: n/a
 
      09-06-2010
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;
 
Reply With Quote
 
 
 
 
Sensei
Guest
Posts: n/a
 
      09-06-2010
On 2010-09-06 14:39:56 +0200, gwowen <> 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.


 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      09-07-2010
gwowen <> 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();
}
//-------------------------------------------------------------------
 
Reply With Quote
 
Bart van Ingen Schenau
Guest
Posts: n/a
 
      09-08-2010
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

> * * }
>
> };
>


Bart v Ingen Schenau
 
Reply With Quote
 
Vladimir Jovic
Guest
Posts: n/a
 
      09-08-2010
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
 
Reply With Quote
 
Alf P. Steinbach /Usenet
Guest
Posts: n/a
 
      09-08-2010
* 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>
 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      09-08-2010
[Please do not mail me a copy of your followup]

"Alf P. Steinbach /Usenet" <alf.p.steinbach+> spake the secret code
<i682ag$4f7$> 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>
 
Reply With Quote
 
Alf P. Steinbach /Usenet
Guest
Posts: n/a
 
      09-08-2010
* Richard, on 08.09.2010 18:26:
> [Please do not mail me a copy of your followup]
>
> "Alf P. Steinbach /Usenet"<alf.p.steinbach+> spake the secret code
> <i682ag$4f7$> 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>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
c++ calling java calling c++ ... Klaus Friese C++ 7 08-15-2005 09:23 PM
calling virtual function results in calling function of base class... Andreas Lagemann C++ 8 01-10-2005 11:03 PM
calling virtual function results in calling function of base class ... tiwy C++ 0 01-09-2005 11:17 PM
Calling FormsAuthentication.SignOut() after calling Response.Flush =?Utf-8?B?TWFydGluIExlZQ==?= ASP .Net 1 09-28-2004 12:47 PM
Server Side button calling page_load before calling it's own click event. Ryan Ternier ASP .Net 4 07-29-2004 01:06 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57