What does it meant when a function member of a class is declared as const?
Thanks
Pmb 15 1127
"Pmb" <so*****@somewhere.com> wrote in message
news:X5********************@comcast.com... What does it meant when a function member of a class is declared as const?
That it does not modify the object on which it is called.
john
Pmb wrote: What does it meant when a function member of a class is declared as const?
That the function is not going to change the state of the
object when called.
In practice this means: This function can be called on const objects.
--
Karl Heinz Buchegger kb******@gascad.at
"Sharad Kala" <no*****************@yahoo.com> wrote in message
news:2h************@uni-berlin.de... "Pmb" <so*****@somewhere.com> wrote in message news:X5********************@comcast.com... What does it meant when a function member of a class is declared as
const? Always check the FAQ first. http://www.parashift.com/c++-faq-lit...html#faq-18.10
Thanks. I checked the FAQ and didn't see that. I searched the page for
"const function" and didn't see it.
In any case, after reading it, I don't understand what it means by "The
'abstract (client-visible) state of the object isn't going to change"
Thanks
Pmb
Pmb wrote: "Sharad Kala" <no*****************@yahoo.com> wrote in message news:2h************@uni-berlin.de... "Pmb" <so*****@somewhere.com> wrote in message news:X5********************@comcast.com... What does it meant when a function member of a class is declared as
const? Always check the FAQ first. http://www.parashift.com/c++-faq-lit...html#faq-18.10
Thanks. I checked the FAQ and didn't see that. I searched the page for "const function" and didn't see it.
In any case, after reading it, I don't understand what it means by "The 'abstract (client-visible) state of the object isn't going to change"
Exactly what it says.
There is a class.
There is an object of this class.
Now this object is used by some client code.
The client code calls a member function of this object.
When calling the member function, the client code will not
notify any changes in the state of that object.
Example:
I write a class which models a person. The state of that person
consists of its name and its birthdate.
I add a member function to that class which allows you to get
the birthdate. But by calling that function, the object will
not change it's visible state: Neither will the name change
nor will the birthdate change. Thus I will make that function
a const one.
--
Karl Heinz Buchegger kb******@gascad.at
"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:40***************@gascad.at... Pmb wrote: What does it meant when a function member of a class is declared as
const?
That the function is not going to change the state of the object when called.
In practice this means: This function can be called on const objects.
I don't understand!? Take the program below as an example. The output is
===
Object Test constructed with x = 1, y = 2
x in print() is 8
y in print() is 9
===
The object "test" was declared constant and yet I modified the two data
members x, y. Does that mean that I've changed the value of the object or
the state of the object.
Perhaps I don't understand what is meant above by the "state of the object"?
Thanks
Pmb
--------------------------------------------------------------------------
#include <iostream.h>
class Test{
public:
Test( int = 0, int = 0 );
void print( int , int ) const;
private:
int x;
int y;
};
Test::Test( int i , int j )
{
x = i;
y = j;
cout << "Object Test constructed with x = " << x << ", y = " << y <<
endl;
}
void Test::print ( int x, int y ) const
{
cout << "x in print() is " << x << endl;
cout << "y in print() is " << y << endl;
}
int main()
{
Test const test( 1, 2 );
test.print ( 8, 9 );
return 0;
}
---------------------------------------------------------------------------
"Pmb" <so*****@somewhere.com> wrote in message
news:m7********************@comcast.com... "Karl Heinz Buchegger" <kb******@gascad.at> wrote in message news:40***************@gascad.at... Pmb wrote: What does it meant when a function member of a class is declared as const? That the function is not going to change the state of the object when called.
In practice this means: This function can be called on const objects.
I don't understand!? Take the program below as an example. The output is
=== Object Test constructed with x = 1, y = 2 x in print() is 8 y in print() is 9 ===
The object "test" was declared constant and yet I modified the two data members x, y.
No you didn't. In print x and y are parameters they are not the member
variables x and y. You wouldnot be able to change the member variables x and
y, but you can change the parameters x and y because they are not declared
const.
Does that mean that I've changed the value of the object or the state of the object.
No you haven't. Perhaps I don't understand what is meant above by the "state of the
object"?
I think you do, the problem it that you don't see that you have two
different x's and two different y's in your program.
john
Pmb wrote: "Karl Heinz Buchegger" <kb******@gascad.at> wrote in message news:40***************@gascad.at... Pmb wrote: What does it meant when a function member of a class is declared as const?
That the function is not going to change the state of the object when called.
In practice this means: This function can be called on const objects.
I don't understand!? Take the program below as an example. The output is
=== Object Test constructed with x = 1, y = 2 x in print() is 8 y in print() is 9 ===
The object "test" was declared constant and yet I modified the two data members x, y.
Where?
You didn't
In the print function you printed the values passed
to that function, not the member variables.
--
Karl Heinz Buchegger kb******@gascad.at
"Pmb" <so*****@somewhere.com> wrote in message
news:JN********************@comcast.com...
[snip] In any case, after reading it, I don't understand what it means by "The 'abstract (client-visible) state of the object isn't going to change"
Say you have a class that contains a char pointer as a member. You allocate
memory for that in the constructor. Later in a member function you only change
the contents of the allocated memory . Now as far as client is concerned there
is constness (no change in value of p though what it points to has changed),
hence it can be declared const.
-Sharad
"John Harrison" <jo*************@hotmail.com> wrote in message
news:2h************@uni-berlin.de... "Pmb" <so*****@somewhere.com> wrote in message news:m7********************@comcast.com... "Karl Heinz Buchegger" <kb******@gascad.at> wrote in message news:40***************@gascad.at... Pmb wrote: > > What does it meant when a function member of a class is declared as const? >
That the function is not going to change the state of the object when called.
In practice this means: This function can be called on const objects.
I don't understand!? Take the program below as an example. The output is
=== Object Test constructed with x = 1, y = 2 x in print() is 8 y in print() is 9 ===
The object "test" was declared constant and yet I modified the two data members x, y.
No you didn't. In print x and y are parameters they are not the member variables x and y. You wouldnot be able to change the member variables x
and y, but you can change the parameters x and y because they are not declared const.
Oops! Thanks
Pmb
Looking at your code bellow int main() { Test const test( 1, 2 );
You made the values of x and y equal to 1 and 2.
But not any x and y, the x and y that belongs to the class Test.
To do that you used to values i and j test.print ( 8, 9 );
now your function prints out two values that you have passed 8 and 9 but to
pass the values you called them x and y in your function.
The fact that the class Test also has two place holders called x and y is
irrelevant really, (but bad programming).
If you change your function to
void 'Test::print ( int i, int j )' const the output will be the value of
the x and y in the class Test, (and i and j would effectively be ignored). return 0;
} --------------------------------------------------------------------------
-
As for a const function it is a function that does not assign values within
it's body.
if you did
void Test::print ( int i, int j ) const
{
x = j;
cout << "x in print() is " << x << endl;
cout << "y in print() is " << y << endl;
}
it would not work because you are trying to change the value of x. That is
not permitted in a const function.
(note that i oversimplified what happens so you get a better idea of what is
going on).
Simon.
Pmb posted: What does it meant when a function member of a class is declared as const?
Thanks
Pmb
int DoStuff(const Dog& doggie)
{
doggie.age = 5;
//ERROR, CANNOT EDIT const OBJECT
return 0;
}
-JKop
"JKop" <NU**@NULL.NULL> wrote in message
news:Nu***************@news.indigo.ie... Pmb posted:
What does it meant when a function member of a class is declared as const?
int DoStuff(const Dog& doggie) { doggie.age = 5;
//ERROR, CANNOT EDIT const OBJECT
return 0;
}
His question is that what is a const member function if you read carefully.
Sharad Kala posted: His question is that what is a const member function if you read carefully.
Opps!!!
int Dog::DoStuff(void) const
{
age = 5;
//ERROR, CANNOT EDIT OBJECT, THIS IS A const FUNCTION!!
return 0;
}
Checklist:
1) Does your function edit object member variables? If not, declare it
const .
3) Does your function neither read nor edit member variables? If so, declare
it static .
-JKop
JKop wrote: ... Checklist:
1) Does your function edit object member variables? If not, declare it const .
Not correct. For example, take a look at non-const version of
'std::vector::operator[]'. It doesn't edit any of the object's member
variables. Do you think it should've been declared as 'const'? What
about non-const versions of 'begin()' and 'end()' methods in standard
containers?
3) Does your function neither read nor edit member variables? If so, declare it static .
Not exactly correct either (for similar reasons).
BTW, where is 2) ?
--
Best regards,
Andrey Tarasevich This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Luis |
last post by:
can you look at my code, and tell
my wy the section of the client program which says :
const Fraction f3(12, 8);
const Fraction f4(202, 303);
result = f3.MultipliedBy(f4);
cout << "The product...
|
by: Thomas Matthews |
last post by:
Hi,
How do I create a const table of pointers to member functions?
I'm implementing a Factory pattern (or jump table). I want to
iterate through the table, calling each member function until
a...
|
by: Corno |
last post by:
Hi all,
There's probably a good reason why a const object can call non const
functions of the objects where it's member pointers point to.
I just don't see it. For me, that makes the the const...
|
by: joe |
last post by:
hi,
after reading some articles and faq,
i want to clarify myself what's correct(conform to standard) and
what's not?
or what should be correct but it isn't simply because compilers don't...
|
by: andrew.fabbro |
last post by:
In a different newsgroup, I was told that a function I'd written that
looked like this:
void myfunc (char * somestring_ptr)
should instead be
void myfunc (const char * somestring_ptr)
...
|
by: Snis Pilbor |
last post by:
Whats the point of making functions which take arguments of a form like
"const char *x"? It appears that this has no effect on the function
actually working and doing its job, ie, if the function...
|
by: Rui.Hu719 |
last post by:
Hi, All:
I read the following passage from a book:
"There are three exceptions to the rule that headers should not contain
definitions: classes, const objects whose value is known at compile...
|
by: Kira Yamato |
last post by:
It is erroneous to think that const objects will have constant behaviors too.
Consider the following snip of code:
class Person
{
public:
Person();
string get_name() const
|
by: Angus |
last post by:
I have a member function, int GetLogLevel() which I thought I should
change to int GetLogLevel() const - I made the change and it works
fine.
But in the function I am creating buffers and of...
|
by: amvoiepd |
last post by:
Hi,
My question is about how to use const properly.
I have two examples describing my problem.
First, let's say I have a linked list and from it I want to
find some special node. I write the...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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...
|
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...
|
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: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
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...
| |