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

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 19001

<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Paul Rubin | last post by:
OK, I want to scan a file for lines matching a certain regexp. I'd like to use an assignment expression, like for line in file: if (g := re.match(pat, line)): croggle(g.group(1)) Since...
14
by: Tony Johansson | last post by:
Hello Experts! Assume I have a class called SphereClass as the base class and a class called BallClass that is derived from the SphereClass. The copy constructor initialize the left hand object...
10
by: Christian Christmann | last post by:
Hi, how do I define an assignment operator which is supposed to copy all member attributes of one object to another where both objects are given as pointers? Example: CLASS_A *source = new...
7
by: skishorev | last post by:
What is the difference between copy initialization and assignment. How the memory will allocates the objects. Thanks &regards, Sai Kishore
13
by: Daniel W | last post by:
Hi! I tried to post this to comp.lang.c.moderated but it didn't seem to go through. I've got a question about volatiles in assignment expressions. I found the following code snippet in an...
7
by: vj | last post by:
Hi Friends, I was going through a C++ reference book when this rule caught my eye: -->Assignment operator '=' is not inherited by the sub class. I cannot figure out why this rule has being...
34
by: Chris | last post by:
Is there ever a reason to declare this as if(*this == rhs) as opposed to what I normally see if(this == &rhs) ?
7
by: blangela | last post by:
What does the default assignment operator (compiler supplied assignment operator, sometimes also referred to as the implicit assignment operator or the synthesized assignment operator) do when the...
61
by: warint | last post by:
My lecturer gave us an assignment. He has a very "mature" way of teaching in that he doesn't care whether people show up, whether they do the assignments, or whether they copy other people's work....
6
by: =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= | last post by:
Yesterday Visual Studio gave me a strange error both at compiletime and at designtime that had no obvious connection to anything I had changed recently. After some effort tracking down the problem...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
marktang
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,...
0
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...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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 project—planning, coding, testing,...

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.