call const function from non const function

F

Fabian Wein

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
 
A

Alf P. Steinbach

* Fabian Wein:
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?

Provide an additional const version of GetList.

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

Neelesh Bodas

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
 
F

Fabian Wein

Alf said:
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
 
F

Fabian Wein

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/library/bz6at95h(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
 
F

Fabian Wein

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

Alf P. Steinbach

* Fabian Wein:
But then I need another return type as the GetList() returns a list of
non-const pointers.

Why would that be a problem?

Code that needs the list count without changing the object, probably
needs the list as const version.

On the other hand, it seems grossly inefficient to retrieve a list only
in order to check the list's length. That suggests a redesign where you
can check the length without retrieving the list.
 
T

terminator

Hi,


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
 

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,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top