473,671 Members | 2,248 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

assignment of read-only variable


Hi,

I have a method like this:

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

And in my caller, i have this:

A* const a = NULL;

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

But I get this compile error:
.../NavBarGroupResu ltListProcessor .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 19034

<yi*****@gmail. com> wrote in message
news:11******** **************@ i39g2000cwa.goo glegroups.com.. .

Hi,

I have a method like this:

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

And in my caller, i have this:

A* const a = NULL;

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

But I get this compile error:
../NavBarGroupResu ltListProcessor .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.c om wrote:
I have a method like this:

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

And in my caller, i have this:

A* const a = NULL;

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

But I get this compile error:
../NavBarGroupResu ltListProcessor .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(aLis t);

// 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(aLis t);

// 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.c om 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(aLis t);

// 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
3222
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 there are no assignment expressions in Python, I have to use a temp var. That's a little more messy, but bearable:
14
2348
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 in this case object b2 below. It also initialize subobject from class SphereClass. The BallClass copy constructor looks like this. BallClass::BallClass(const BallClass& bc) : SphereClass(bc)
10
2591
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 CLASS_A; ....
7
3728
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
1935
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 embedded development forum: ----
7
11021
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 imposed and how it actually makes any sense. If operator '=' cannot be inherited then it should not be allowed to be made virtual . But following declaration on VC 6.0 compiler gets through :
34
7165
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
1785
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 class contains a reference. For example if the iref member of object A references X and the iref member of B references Y, if we then go: A = B; will the iref member of A now reference X or Y.
61
3539
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. Furthermore, he doesn't even mark the assignments, but rather gives tips and so forth when going over students' work. To test students' capabilities for the purpose of state exams and qualifications though, he actually sits down with us at a...
6
3336
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 I discovered first a workaround, then the real cause of the problem. I would like to understand why what I am doing is frowned upon by Visual Studio and how to do this properly. My application is in one solution; supporting libraries including...
0
8393
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8917
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...
1
8598
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7437
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, and deployment—without 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
6229
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
5696
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();...
1
2812
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1809
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.