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

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 1859
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_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
--
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
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...
2
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: ...
9
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...
15
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...
2
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...
4
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 =...
10
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
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...
12
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:...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.