Query about const member function

B

Bala L

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

Victor Bazarov

Bala said:
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
 
B

Bala L

Victor said:
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.


That shouldn't matter.

V

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?
 
V

Victor Bazarov

Bala said:
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_member() const {
this->somepointer = &someotherobject; // 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
 
M

mlimber

Bala said:
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-lite/containers.html#faq-34.1).

Cheers! --M
 
S

S S

mlimber said:
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
 
V

Victor Bazarov

S said:
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
 
B

Bala L

Looks like I confused pointer-to-a-constant with constant pointers.
Thanks for the clarification!

Victor said:
S said:
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
 

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

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top