473,395 Members | 1,568 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,395 software developers and data experts.

Funky function

Hi,
What does the keyword const do for this function?

virtual bool isUndoable() const { return true;}

Daniel
Jul 22 '05 #1
4 1813
Daniel wrote:
Hi,
What does the keyword const do for this function?

virtual bool isUndoable() const { return true;}


It tells the compiler that the function does not modify the object it is
called for.

Jul 22 '05 #2
Rolf Magnus wrote:

Daniel wrote:
Hi,
What does the keyword const do for this function?

virtual bool isUndoable() const { return true;}


It tells the compiler that the function does not modify the object it is
called for.


To elaborate a bit more for the OP:
It makes it possible to call this member function on a const object
or through a pointer or reference to a const object (the pointed to
object may or may not be const). That would not be allowed otherwise,
without a const qualifier.
It is possible, moral and typical for a const member function to
modify data members declared mutable.
Technically, it is possible for a const member function to directly
modify non-const non-mutable data members of a non-const object of
its class but only by casting away the constness (check out the thread
"const_cast, reinterpret_cast"). If the object itself or the data
member is const, this will cause undefined behaviour.

Denis
Jul 22 '05 #3
"Denis Remezov" <fi***************@yahoo.removethis.ca> wrote in message
news:40***************@yahoo.removethis.ca...
Rolf Magnus wrote:

Daniel wrote:
Hi,
What does the keyword const do for this function?

virtual bool isUndoable() const { return true;}


It tells the compiler that the function does not modify the object it is
called for.


To elaborate a bit more for the OP:
It makes it possible to call this member function on a const object
or through a pointer or reference to a const object (the pointed to
object may or may not be const). That would not be allowed otherwise,
without a const qualifier.
It is possible, moral and typical for a const member function to
modify data members declared mutable.
Technically, it is possible for a const member function to directly
modify non-const non-mutable data members of a non-const object of
its class but only by casting away the constness (check out the thread
"const_cast, reinterpret_cast"). If the object itself or the data
member is const, this will cause undefined behaviour.


Wow! Uuuuhhh... can anyone translate Klingon to English; cos that was
Klingon to me!

--
Mabden
Jul 22 '05 #4
Mabden wrote:

"Denis Remezov" <fi***************@yahoo.removethis.ca> wrote in message
news:40***************@yahoo.removethis.ca...
Rolf Magnus wrote:

Daniel wrote:

> Hi,
> What does the keyword const do for this function?
>
> virtual bool isUndoable() const { return true;}

It tells the compiler that the function does not modify the object it is
called for.


To elaborate a bit more for the OP:
It makes it possible to call this member function on a const object
or through a pointer or reference to a const object (the pointed to
object may or may not be const). That would not be allowed otherwise,
without a const qualifier.
It is possible, moral and typical for a const member function to
modify data members declared mutable.
Technically, it is possible for a const member function to directly
modify non-const non-mutable data members of a non-const object of
its class but only by casting away the constness (check out the thread
"const_cast, reinterpret_cast"). If the object itself or the data
member is const, this will cause undefined behaviour.


Wow! Uuuuhhh... can anyone translate Klingon to English; cos that was
Klingon to me!


Maybe a book written in english about C++ would not hurt.
The above is crystal clear :-)

OK. Simple.

Say you have a class (I leave out some details such as initialization
and just concentrate on the topic)

class Buffer
{
protected:
int Data;
int Time;
};

that has a member function:

class Buffer
{
public:
int foo() { return Data );

protected:
int Data;
int Time;
};

and now you use an instance of that class somwhere

int main()
{
const Buffer MyData;

cout << MyData.foo();
}

Then the compiler has a problem. In main you told the
compiler: MyData is constant. It will not change. But
in the next line you try to call a function foo on MyData.
Now how should the compiler know that foo() will not do
what the declaration promised: Don't change MyData. In
general it can't. Thecompiler might not see the implementation
of foo() at the point of call and thus cannot analyze if foo()
will not break the contract: MyData is constant and will not change.

The solution: Tell the compiler that foo() behaves and does not
change the object. That's what the const after the functions signature
is all about:

class Buffer
{
public:
int foo() const { return Data );

protected:
int Data;
int Time;
};

When doing this, the compiler, when compiling foo, ensures that
none of the statements in foo() attempt to do a change in the
Buffer object.

class Buffer
{
public:
//
// the following is illegal.
// the function promises to not change the class intrnals
// but then tries to do so by assigning to Time
//
int foo() const { Time = 5; return Data );

protected:
int Data;
int Time;
};

But sometimes this is not the end of the story.
The point is: From the perspective of the user of an Buffer object,
the internals of that object are unimportant. It is the observable
behaviour that counts. That said: If foo() changes the Time variable
in MyData is completely unimportant, from the point of main() the MyData
object still hasn't changed, since there is no way to get at it.

A solution to this is to make Time a mutable member.

class Buffer
{
public:
int foo() const { return Data );

protected:
int Data;
mutable int Time;
};

This tells the compiler: Time may change, even for a constant object.
Thus:

class Buffer
{
public:
int foo() const { Time = 5; return Data );

protected:
int Data;
int Time;
};

becomes legal. It is as saying: foo() will not change the state
of the object, with the exception of Time. But since there is
no way for the user of this class to figure out that change,
it is ok.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #5

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

Similar topics

2
by: Tom Plunket | last post by:
(Is this an appropriate place to ask wxPython questions?) Here's some code (line breaks put in without testing them, I'm kinda a newbie hope they're ok): <busted.py> from wxPython.wx import...
7
by: Jonas Galvez | last post by:
Hi, I need a function to parse badly encoded 'Subject' headers from e-mails, such as the following: =?ISO-8859-1?Q?Murilo_Corr=EAa?= =?ISO-8859-1?Q?Marcos_Mendon=E7a?= I tried using the...
7
by: Pete | last post by:
Quick question: What does ary return in C/C++? I know that C/C++ does not have two-dimensional arrays, thus ary is not valid notation. Instead I should use ary . But. But I was...
5
by: Csaba2000 | last post by:
I thought assigning one function to another was fairly straightforward, as the example below illustrates. So I was really surprised that IE 6 showed the alert while Firefox 1.0.1 failed. It's...
1
by: gene.ellis | last post by:
Put simply, I have a text box, and people commonly cut + paste information into this text box from Microsoft word. The problem is that word has all types of funky characters (smart quotes,...
3
by: Tracy | last post by:
Just recently have I noticed that when we run our production application it is adding some funky directory to the address, (fwuhjo45ohmpj35552vwim55), which doesn't exist but it still works. This...
12
by: comp.lang.php | last post by:
I have a textarea where people can cut & paste their resume. Unfortunately they often cut & paste their Word resume into the textarea, funky characters and all. This causes the display to be...
3
by: VirGin | last post by:
I am passing an object by reference to a function that is supposed to populate it with current data. Both the destination object and the source have nested std::lists, some branches of the lists...
12
Plater
by: Plater | last post by:
Well, I'm stuck asking you all, because I cannot find a piece of code that works in multiple browsers. I have the following code that works in google chrome and ie: //Add a keypress watcher to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
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...

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.