473,324 Members | 2,567 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

call const function from non const function

Hi,

is there a way to call a const function from non const function?

I have a non-const

List GetList();

and want my

bool Count() const;

implemented as

bool Foo::Count(...) const
{
return GetList().GetSize();
}

Is there any not to dirty cast trick?

Thanks, Fabian
Jul 25 '07 #1
6 6435
Muk
Is GetList() a member function of class Foo? The const keyword on
Count() is telling the compiler that Count() will not modify Foo. If
GetList() is a member of Foo and does modify Foo you could use a
const_cast to solve your problem. See a similar example here --->
http://msdn2.microsoft.com/en-us/lib...5h(VS.80).aspx

On Jul 25, 12:47 pm, Fabian Wein <fw...@lse.e-technik.uni-erlangen.de>
wrote:
Hi,

is there a way to call a const function from non const function?

I have a non-const

List GetList();

and want my

bool Count() const;

implemented as

bool Foo::Count(...) const
{
return GetList().GetSize();

}

Is there any not to dirty cast trick?

Thanks, Fabian

Jul 25 '07 #2
On Jul 25, 9:47 pm, Fabian Wein <fw...@lse.e-technik.uni-erlangen.de>
wrote:
Hi,

is there a way to call a const function from non const function?
A const function can always be called from a non-const function.

You probably meant the reverse way: "calling a non-const function from
a const function".
I have a non-const

List GetList();

and want my

bool Count() const;

implemented as

bool Foo::Count(...) const
{
return GetList().GetSize();
If your GetList() is changing the logical constness of the object,
then your Count function, which relies on GetList, should, at the
first place, be non-const.

Alternatively, if the Count function, as the name indicates, is
supposed to return just a count (or do anything that doesnot alter
logical constness of the object) then you should probably provide a
way to do this through helper functions that donot alter the logical
constness.

In other words, if you are trying to define a const function in terms
of a non-const function, you probably need to modify the design.

-N

Jul 25 '07 #3
Alf P. Steinbach wrote:
Provide an additional const version of GetList.
But then I need another return type as the GetList() returns a list of
non-const pointers.
There is a trick for doing that via templates, but it's generally not
used because it doesn't pay enough. Also, there have been suggestions
for additional syntax to handle that code duplication. All such
suggestions have resulted in exactly nothing: there's no real demand.
To be honest I also rarely have such a problem.

Regards, Fabian

Jul 26 '07 #4
Hi,
Is GetList() a member function of class Foo? The const keyword on
Count() is telling the compiler that Count() will not modify Foo. If
GetList() is a member of Foo and does modify Foo you could use a
const_cast to solve your problem. See a similar example here --->
http://msdn2.microsoft.com/en-us/lib...5h(VS.80).aspx
Yest, it is Vector<Pointer*Foo::GetList().

GetList() cannot be const as one could modify the object via
the returned pointers.

But my Count()

bool Foo::Count(...) const
{
return GetList().GetSize();
}

would make sure that the object is not altered.

I don't see how I could use cost_cast here.

Fabian
Jul 26 '07 #5
Hi,
A const function can always be called from a non-const function.

You probably meant the reverse way: "calling a non-const function from
a const function".
Sorry - your are perfeclty right.
>
>I have a non-const

List GetList();

and want my

bool Count() const;

implemented as

bool Foo::Count(...) const
{
return GetList().GetSize();

If your GetList() is changing the logical constness of the object,
then your Count function, which relies on GetList, should, at the
first place, be non-const.
GetList() returns pointers to content of the object. Hence one
could alter the object via the return value and such the GetList()
cannot be const. My Count() implementation would assure that
the object is not altered.
Alternatively, if the Count function, as the name indicates, is
supposed to return just a count (or do anything that doesnot alter
logical constness of the object) then you should probably provide a
way to do this through helper functions that donot alter the logical
constness.

In other words, if you are trying to define a const function in terms
of a non-const function, you probably need to modify the design.
I find my intendet design good:

Vector<Pointers*Foo::GetList()
cannot be const due to the result alowing modification.

int Foo::Count() const
{ return GetList().GetSize(); }

or bool Foo::Has() const
{ return GetList().GetSize() 0; }

would not involve copy & paste code.

I ommited the parameters of the methods.

Regards, Fabian
Jul 26 '07 #6
On Jul 26, 10:19 am, Fabian Wein <fw...@lse.e-technik.uni-erlangen.de>
wrote:
Hi,
Is GetList() a member function of class Foo? The const keyword on
Count() is telling the compiler that Count() will not modify Foo. If
GetList() is a member of Foo and does modify Foo you could use a
const_cast to solve your problem. See a similar example here --->
http://msdn2.microsoft.com/en-us/lib...5h(VS.80).aspx

Yest, it is Vector<Pointer*Foo::GetList().

GetList() cannot be const as one could modify the object via
the returned pointers.
you had better provide a 'const' version for 'GetList()' but I you
will face the trouble of calling a none-const function inside a
'const' one,but this is just like calling a function taking a none-
const object on a const one.In your code the const object is of the
type that owns the member function:

Vector<Pointer*Foo::GetList();
const Vector<Pointer*Foo::GetList()const{
return const_cast<Foo*>(this)->GetList();
};

but this is really bad manors since the vector - which may be a very
large one - is copied which has both overheads: memory and runtime.
>
But my Count()

bool Foo::Count(...) const
{
return GetList().GetSize();

}

would make sure that the object is not altered.

I don't see how I could use cost_cast here.
you need operate on 'this' since 'bool Foo::Count() const' just
means:'const Foo* this'.
const_cast<Foo*>(this)->GetList().GetSize();//cast 'this' from 'const
Foo*' to 'Foo*'

but you had better try to avoid such casts.

regards,
FM

Jul 26 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: cheeser | last post by:
Hello all, Please see the question in the code below... Thanks! Dave #include <iostream>
4
by: mangi03 | last post by:
Hi, I came acrosss g++ compile errors whenever I make a function call by reference and found out from the test program that compiler is treating the function argument differently when another...
3
by: Dave C | last post by:
I've written the following code that won't compile, trimmed down to just the pertinent stuff: --- WindowClass.hxx ---------------------------------------------------- #include <set> class...
5
by: Rob | last post by:
Help me, I'm just beginning with programming in Access 2000. I've tried the http://www.mvps.org/access/api/api0001.htm but it won't work in Access. What am i doing wrong. I don't have...
5
by: SStory | last post by:
Hi all, I really needed to get the icons associated with each file that I want to show in a listview. I used the follow modified code sniplets found on the internet. I have left in...
2
by: waitan | last post by:
#include <algorithm> #include <iostream> #include <fstream> #include <string> #include <vector> #include <sstream> #include <iterator> #include <iomanip> using namespace std;
3
by: msnews.microsoft.com | last post by:
Hi i am using User32.dll in Visual stdio 2005. public static extern long SetActiveWindow(long hwnd); public static extern long keybd_event(byte bVk, byte bScan, long dwFlags,
2
by: John M. Gamble | last post by:
I'm getting this message in Visual Studio 2005: PInvokeStackImbalance was detected Message: A call to PInvoke function 'Refresh!Refresh.Main::WNetAddConnection2' has unbalanced the stack. ...
2
by: cablepuff | last post by:
template <typename ContainerType> ContainerType rsa_encrypt_list(const std::string&, const typename ContainerType::reference, const typename ContainerType::reference); const BigInteger...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.