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? 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 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)
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?
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. 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
by: skishorev |
last post by:
What is the difference between copy initialization and assignment.
How the memory will allocates the objects.
Thanks ®ards,
Sai Kishore
|
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...
|
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...
|
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)
?
|
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...
|
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....
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
| |