470,811 Members | 1,014 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,811 developers. It's quick & easy.

assignment of read-only variable


Hi,

I have a method like this:

const A* getMaxArea(const vector<A*>& aList);

And in my caller, i have this:

A* const a = NULL;

if (!aList.empty()) {
a = getMaxArea(aList);
//.....
}

But I get this compile error:
.../NavBarGroupResultListProcessor.cpp:49: error: assignment of
read-only variable 'a'

Can you please tell me what is the pointer,
i think
A* const a // is a pointer to 'const a', not a const pointer to 'a', so
I should be able to assign it, right?

Apr 29 '06 #1
5 18040

<yi*****@gmail.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com...

Hi,

I have a method like this:

const A* getMaxArea(const vector<A*>& aList);

And in my caller, i have this:

A* const a = NULL;

if (!aList.empty()) {
a = getMaxArea(aList);
//.....
}

But I get this compile error:
../NavBarGroupResultListProcessor.cpp:49: error: assignment of
read-only variable 'a'

Can you please tell me what is the pointer,
i think
A* const a // is a pointer to 'const a', not a const pointer to 'a', so
I should be able to assign it, right?


I believe it should be read from right to left: 'a' is a const pointer to
'A'. Therefore, 'a' (the pointer) is constant and may not be re-assigned.

- Dennis
Apr 29 '06 #2
yi*****@gmail.com wrote:
I have a method like this:

const A* getMaxArea(const vector<A*>& aList);

And in my caller, i have this:

A* const a = NULL;

if (!aList.empty()) {
a = getMaxArea(aList);
//.....
}

But I get this compile error:
../NavBarGroupResultListProcessor.cpp:49: error: assignment of
read-only variable 'a'

Can you please tell me what is the pointer,
i think
A* const a // is a pointer to 'const a', not a const pointer to 'a',
so I should be able to assign it, right?


No, it is a const pointer to an object of type 'A'. Always read the
types from right to left.

Changing the type to 'A const*' will fix your problem, as far as I
can see.

hth
--
jb

(reply address in rot13, unscramble first)
Apr 29 '06 #3
Thanks. It works.

I have another question:

I have another funcion, which is this:

void f1(const A* aPtr);

A const *a = NULL;

if (!aList.empty()) {
a = getMaxArea(aList);

// try to call f1() here,
const A* a1 = a;
f1 (a1); // won't compile due to error: invalid conversion from
'const A*' to 'A*'
}
Any idea?

Apr 29 '06 #4
On Sat, 29 Apr 2006 09:29:44 -0700, yinglcs wrote:
Thanks. It works.

I have another question:

I have another funcion, which is this:

void f1(const A* aPtr);

A const *a = NULL;

if (!aList.empty()) {
a = getMaxArea(aList);

// try to call f1() here,
const A* a1 = a;
f1 (a1); // won't compile due to error: invalid conversion from
'const A*' to 'A*'
}
Any idea?


Yes, your f1() function isn't declared to take a pointer to const.
Apr 29 '06 #5
yi*****@gmail.com wrote:
Thanks. It works.

I have another question:

I have another funcion, which is this:

void f1(const A* aPtr);

A const *a = NULL;

if (!aList.empty()) {
a = getMaxArea(aList);

// try to call f1() here,
const A* a1 = a;
f1 (a1); // won't compile due to error: invalid conversion from
'const A*' to 'A*'
}
Any idea?


Are you sure the code above is actually the code that won't compile?
After writing code to put it in a suitable context it compiled just fine
for me. (By the way, when posting example code it helps a lot if you
provide something that can be directly cut and paste without any
additional context needed.)

For what it's worth, "const A *" and "A const *" are the same type, both
meaning "pointer to a const A".

--
Alan Johnson
Apr 29 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

23 posts views Thread by Paul Rubin | last post: by
14 posts views Thread by Tony Johansson | last post: by
10 posts views Thread by Christian Christmann | last post: by
7 posts views Thread by skishorev | last post: by
13 posts views Thread by Daniel W | last post: by
34 posts views Thread by Chris | last post: by
61 posts views Thread by warint | last post: by
6 posts views Thread by =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.