473,791 Members | 3,074 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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().GetSi ze();
}

Is there any not to dirty cast trick?

Thanks, Fabian
Jul 25 '07 #1
6 6480
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().GetSi ze();

}

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().GetSi ze();
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().GetSi ze();
}

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().GetSi ze();

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().GetSi ze(); }

or bool Foo::Has() const
{ return GetList().GetSi ze() 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()c onst{
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().GetSi ze();

}

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().GetS ize();//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
3280
by: cheeser | last post by:
Hello all, Please see the question in the code below... Thanks! Dave #include <iostream>
4
3396
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 function call funcRet()is made which returns the expected argument type for the function call by reference funcByRef(class A&); The only way to get around this probelm is to first call the funcRet(), assign its value to a variable and pass that...
3
1507
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 Window; class WindowClass { public:
5
2972
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 problems with the http://www.mvps.org/access/api/api0002.htm but it only browse to folders.
5
2319
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 commented code for anyone else who may be looking to do the same.
2
2958
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
16058
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
2663
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. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.
2
6569
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 e(boost::lexical_cast<BigInteger>(rsa_encrypts)); const BigInteger n(boost::lexical_cast<BigInteger>(rsa_encrypts)); std::string infile(rsa_encrypts); boost::scoped_ptr<boost::filesystem::ifstreamencrypt_input(new boost::filesystem::ifstream(infile));
0
9669
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10428
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10207
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10156
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9997
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7537
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5435
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4110
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2916
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.