473,766 Members | 2,172 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Query about const member function

I have a class with a private array data member 'm_array'. I have
written a public function, called 'fileRead', to read values into the
array from a file. I just noticed that I have declared this function to
be const.

I dont understand how the code compiled and executed without returning
an error or a warning. I am assigning values from a file to m_array
inside this function. I thought that a member function can be made
const only if *this (this pointer) is not being modified. Am I missing
something?

Btw, I am using the Visual C++ .NET environment to run the code.

Oct 23 '06 #1
7 1891
Bala L wrote:
I have a class with a private array data member 'm_array'. I have
written a public function, called 'fileRead', to read values into the
array from a file. I just noticed that I have declared this function
to be const.

I dont understand how the code compiled and executed without returning
an error or a warning. I am assigning values from a file to m_array
inside this function. I thought that a member function can be made
const only if *this (this pointer) is not being modified. Am I missing
something?
I don't know. What *we* are missing is the actual code. For all we
know the alleged "array" could be a pointer to a dynamic array. Then
when you're modifying the elements (by assigning to them), you aren't
changing the instance, only the memory to which the pointer points.

Read the FAQ 5.8.
Btw, I am using the Visual C++ .NET environment to run the code.
That shouldn't matter.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 23 '06 #2
Victor Bazarov wrote:
Bala L wrote:
I have a class with a private array data member 'm_array'. I have
written a public function, called 'fileRead', to read values into the
array from a file. I just noticed that I have declared this function
to be const.

I dont understand how the code compiled and executed without returning
an error or a warning. I am assigning values from a file to m_array
inside this function. I thought that a member function can be made
const only if *this (this pointer) is not being modified. Am I missing
something?

I don't know. What *we* are missing is the actual code. For all we
know the alleged "array" could be a pointer to a dynamic array. Then
when you're modifying the elements (by assigning to them), you aren't
changing the instance, only the memory to which the pointer points.

Read the FAQ 5.8.
Btw, I am using the Visual C++ .NET environment to run the code.

That shouldn't matter.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Yes, the array is dynamically created.

Since the this-pointer is supposed to be maintained constant by
declaring the member function to be const, I thought all the class
members will be maintained constant. Does this mean that the const-ness
is only defined for non-pointer datatypes?

Oct 23 '06 #3
Bala L wrote:
Victor Bazarov wrote:
>Bala L wrote:
>>I have a class with a private array data member 'm_array'. I have
written a public function, called 'fileRead', to read values into
the array from a file. I just noticed that I have declared this
function to be const.

I dont understand how the code compiled and executed without
returning an error or a warning. I am assigning values from a file
to m_array inside this function. I thought that a member function
can be made const only if *this (this pointer) is not being
modified. Am I missing something?

I don't know. What *we* are missing is the actual code. For all we
know the alleged "array" could be a pointer to a dynamic array. Then
when you're modifying the elements (by assigning to them), you aren't
changing the instance, only the memory to which the pointer points.

Read the FAQ 5.8.
>>Btw, I am using the Visual C++ .NET environment to run the code.

That shouldn't matter.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Yes, the array is dynamically created.

Since the this-pointer is supposed to be maintained constant by
declaring the member function to be const, I thought all the class
members will be maintained constant. Does this mean that the
const-ness is only defined for non-pointer datatypes?
What are you talking about? Your pointer remains constant. What
it points to was never declared constant in the first place. In the
member function declared 'const', the 'this' is const, and so is every
*immediate* object you can refer to using 'this':

void foo_const_membe r() const {
this->somepointer = &someotherobjec t; // cannot do that
this->somepointer[42] = 10; // no problem
}

IOW, declaring a member function 'const' instructs your compiler to
flag every attempt to change a *member* of that class. Elements of
an array to which your pointer points are *NOT* members of your
object. They are fair game.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 23 '06 #4
Bala L wrote:
I have a class with a private array data member 'm_array'. I have
written a public function, called 'fileRead', to read values into the
array from a file. I just noticed that I have declared this function to
be const.

I dont understand how the code compiled and executed without returning
an error or a warning. I am assigning values from a file to m_array
inside this function. I thought that a member function can be made
const only if *this (this pointer) is not being modified. Am I missing
something?

Btw, I am using the Visual C++ .NET environment to run the code.
When you have a pointer as a data member, the pointer itself is made
const when you are in a const function but the const-ness is not
propogated to pointee. Thus:

class A
{
int *pi;

// ...

void Foo() const
{
pi = 0; // Error, pi is const
*pi = 0; // Ok, *pi is non-const
}

void Foo()
{
pi = 0; // Ok, pi is non-const
*pi = 0; // Ok, *pi is non-const
}
};

If you want const to be propogated, use a container such as
std::vector. In fact, you should almost certainly be using such a
container for dynamically allocated arrays anyway (see
http://www.parashift.com/c++-faq-lit...html#faq-34.1).

Cheers! --M

Oct 23 '06 #5
S S

mlimber wrote:
Bala L wrote:
I have a class with a private array data member 'm_array'. I have
written a public function, called 'fileRead', to read values into the
array from a file. I just noticed that I have declared this function to
be const.

I dont understand how the code compiled and executed without returning
an error or a warning. I am assigning values from a file to m_array
inside this function. I thought that a member function can be made
const only if *this (this pointer) is not being modified. Am I missing
something?

Btw, I am using the Visual C++ .NET environment to run the code.

When you have a pointer as a data member, the pointer itself is made
const when you are in a const function but the const-ness is not
propogated to pointee. Thus:

class A
{
int *pi;

// ...

void Foo() const
{
pi = 0; // Error, pi is const
*pi = 0; // Ok, *pi is non-const
Wrong!!!, no memory is allocated for above statement to be true, you
could do
pi = new int;
*pi = 0; // Now OK
}

void Foo()
{
pi = 0; // Ok, pi is non-const
*pi = 0; // Ok, *pi is non-const
Same reason
}
};

If you want const to be propogated, use a container such as
std::vector. In fact, you should almost certainly be using such a
container for dynamically allocated arrays anyway (see
http://www.parashift.com/c++-faq-lit...html#faq-34.1).

Cheers! --M
Oct 24 '06 #6
S S wrote:
mlimber wrote:
>Bala L wrote:
>>I have a class with a private array data member 'm_array'. I have
written a public function, called 'fileRead', to read values into
the array from a file. I just noticed that I have declared this
function to be const.

I dont understand how the code compiled and executed without
returning an error or a warning. I am assigning values from a file
to m_array inside this function. I thought that a member function
can be made const only if *this (this pointer) is not being
modified. Am I missing something?

Btw, I am using the Visual C++ .NET environment to run the code.

When you have a pointer as a data member, the pointer itself is made
const when you are in a const function but the const-ness is not
propogated to pointee. Thus:

class A
{
int *pi;

// ...

void Foo() const
{
pi = 0; // Error, pi is const
*pi = 0; // Ok, *pi is non-const

Wrong!!!, no memory is allocated for above statement to be true
[..]

WHAT? The "// ..." clearly contains the necessary code to allocate
all the necessary memory.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 24 '06 #7
Looks like I confused pointer-to-a-constant with constant pointers.
Thanks for the clarification!

Victor Bazarov wrote:
S S wrote:
mlimber wrote:
Bala L wrote:
I have a class with a private array data member 'm_array'. I have
written a public function, called 'fileRead', to read values into
the array from a file. I just noticed that I have declared this
function to be const.

I dont understand how the code compiled and executed without
returning an error or a warning. I am assigning values from a file
to m_array inside this function. I thought that a member function
can be made const only if *this (this pointer) is not being
modified. Am I missing something?

Btw, I am using the Visual C++ .NET environment to run the code.

When you have a pointer as a data member, the pointer itself is made
const when you are in a const function but the const-ness is not
propogated to pointee. Thus:

class A
{
int *pi;

// ...

void Foo() const
{
pi = 0; // Error, pi is const
*pi = 0; // Ok, *pi is non-const
Wrong!!!, no memory is allocated for above statement to be true
[..]

WHAT? The "// ..." clearly contains the necessary code to allocate
all the necessary memory.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 24 '06 #8

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

Similar topics

2
3638
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 support. (first i compiled them with g++3.x. ERR means compiler will bark, otherwise it does accept it. Then the Comeau C/C++ 4.3.3 comes)
2
2287
by: jsnX | last post by:
i want a function object that is a) initialized with an STL container foo b) will search foo for an object of type foo::value_type here is my code: ======================================================================== /* if we have a big list of things, and we went to check it * over and over for this or that thing, then we can use this * object to cache the list and consolidate queries of it.
9
8256
by: tropostropos | last post by:
On Solaris, using the Sun compiler, I get annoying warnings from the following code. The problem is that I am passing a C++ member function pointer to the C library function qsort. Is there a solution? Declaring the function extern "C" fails, because linkage declarations must be made at file scope. #include <stdlib.h> //for qsort template <class T> class Sorter
15
2141
by: Jiří Paleček | last post by:
Hello, I know the rules for const handling in C++, but I'd like to ask what is the "right" way to use them, eg. when is it appropriate to make a member function const? This came across this question when I was thinking about implementation of a class implementing some lazy data structure or cache. The C++ rules allow among other possibilities making all member functions non-const, or making all member functions const and all members...
2
2516
by: Lionel B | last post by:
I have a function which takes a functor argument. I which to call it for a functor which is actually a class member; this works fine, using the mem_fun_ref and bind1st functions (see listing 1 below). Or, rather, it works fine as long as my member functor is const. The problem comes when I wish to use it for a *non*-const functor (see listing 2 below): *** Start listing 1 *************************************************** // test1.cpp
4
6696
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader = "Content-Type: text/html\r\n"; Why is the second const needed and what does it do? Thanks
10
2186
by: subramanian100in | last post by:
The following is a beginner's question. Suppose TYPE1 and TYPE2 are two types for which suitable ctors and operator= are defined. Suppose I have class Test { TYPE1 mem1;
2
1932
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 course the buffers are filling up with data. So some variable values are changing. So what is rule for a const member function? Is it that only member variables cannot change? But local variables inside the function can?
12
6252
by: hweekuan | last post by:
hi, it seems i can't assign the const variable u in class A, one way to solve the problem may be to build a copy constructor. however, why does C++ or vector class not like this code? my g++ is: gcc version 4.0.1 (Apple Inc. build 5465). thanks for the help. summary of compile error: --------------------------------------- cpp.C:4: error: non-static const member 'const unsigned int A::u',
0
10168
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
10009
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...
0
9838
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...
0
8835
agi2029
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 projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7381
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
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
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.