473,406 Members | 2,369 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,406 software developers and data experts.

Question regarding operator[]() and const

I was trying out the binary operators
and did the following:

class mv
{
private:
int* Storage;
int StSize;
public:
mv(int Size = 0);
~mv();
mv(const mv& Right);
mv& operator=(const mv& Right);
int& operator[](int Position);
int Size() const;
};

mv operator+(const mv& Left, const mv& Right)
{
int ResultSize;
if (Left.Size() < Right.Size())
{
ResultSize = Left.Size();
}
else
{
ResultSize = Right.Size();
}
mv Result(ResultSize);
for (int i = 0; i < ResultSize; ++i)
{
Result[i] = Left[i] + Right[i];
}
return (Result);
}

Which resulted in a compaint that a non const function was being
called by a const object.

so I altered
int& operator[](int Position);
to
int& operator[](int Position) const;

But when I did this I expected the compiler to complain that I was
trying to alter Result[i] while it was constant.
It didn't. Why not?
Jul 22 '05 #1
4 1401
ve*********@hotmail.com (velthuijsen) wrote in
news:e5**************************@posting.google.c om:
I was trying out the binary operators
and did the following:

class mv
{
private:
int* Storage;
int StSize;
public:
mv(int Size = 0);
~mv();
mv(const mv& Right);
mv& operator=(const mv& Right);
int& operator[](int Position);
int Size() const;
};


(...)

You should provide two operator[] overloads for const and non-const
objects.

--
:: bartekd [at] o2 [dot] pl

Jul 22 '05 #2
>
Which resulted in a compaint that a non const function was being
called by a const object.

so I altered
int& operator[](int Position);
to
int& operator[](int Position) const;

But when I did this I expected the compiler to complain that I was
trying to alter Result[i] while it was constant.
It didn't. Why not?


You cannot return a non-const reference to a member from a const method. If
your code was allowed then this would compile

const mv x;
x[0] = 1; // modifying const object

Which I'm sure you agree makes no sense at all.

The answer is to have *two* operator[] defined, one const and one non-const.
Like this

int& operator[](int Position); // your original non-const

int operator[](int Position) const; // new const version

john
Jul 22 '05 #3
velthuijsen wrote in news:e5**************************@posting.google.c om
in comp.lang.c++:
I was trying out the binary operators
and did the following:

class mv
{
private:
int* Storage;
int StSize;
public:
mv(int Size = 0);
~mv();
mv(const mv& Right);
mv& operator=(const mv& Right);
int& operator[](int Position);
int Size() const;
};

mv operator+(const mv& Left, const mv& Right)
{
int ResultSize;
if (Left.Size() < Right.Size())
{
ResultSize = Left.Size();
}
else
{
ResultSize = Right.Size();
}
mv Result(ResultSize);
for (int i = 0; i < ResultSize; ++i)
{
Result[i] = Left[i] + Right[i];
}
return (Result);
}

Which resulted in a compaint that a non const function was being
called by a const object.

so I altered
int& operator[](int Position);
to
int& operator[](int Position) const;

But when I did this I expected the compiler to complain that I was
trying to alter Result[i] while it was constant.
It didn't. Why not?


Because you didn't ask it too, try:

int const & operator [] ( int Position ) const;

Note that you can have *both* const and non-const vertion's
of this member function in "mv", if you find that appropriate.

HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #4
> You cannot return a non-const reference to a member from a const method. If
your code was allowed then this would compile

const mv x;
x[0] = 1; // modifying const object

Which I'm sure you agree makes no sense at all.
Yes that is why I asked what was wrong.
The answer is to have *two* operator[] defined, one const and one non-const.
Like this

int& operator[](int Position); // your original non-const

int operator[](int Position) const; // new const version

john


Neither one would complain. I was expecting that the second one would
complain if I would have tried result[i] = i.
But as Willcroft pointed out I should have made it
int& const operator[](int Position) const;

The problem I had was that I hadn't realised that by doing result[i] =
i I was modifying the reference returned, not the operator[] function.
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Jessica | last post by:
Hi, I have a design question. I am making a time series analysis tool. Since I already use STL vector to represent time series, is there a need to implement a class for the time series object? ...
1
by: Sean W. Quinn | last post by:
Hey folks, I have a question regarding file handling, and the preservation of class structure. I have a class (and I will post snippets of code later in the post) with both primitive data...
6
by: Ben Ingram | last post by:
Hi all, I am writing a template matrix class in which the template parameters are the number of rows and number of columns. There are a number of reasons why this is an appropriate tradeoff for...
5
by: Riku Jarvinen | last post by:
Hi everyone, I asked another question regarding this same subject about a week ago. See thread: ...
2
by: Harry | last post by:
Hi all, I am writing a logger program which can take any datatype. namespace recordLog { enum Debug_Level {low, midium, high}; class L { std::ofstream os; Debug_Level cdl; const...
17
by: Ashwin | last post by:
hi guys, i have overloaded the << operator.as shown below. ostream& operator<<(ostream &out, const student &a) { out<<a.idno; out<< " " ; // out<< a.name; out<< " " ; // out<< a.marks...
8
by: indrawati.yahya | last post by:
In a recent job interview, the interviewer asked me how I'd design classes for the following problem: let's consider a hypothetical firewall, which filters network packets by either IP address,...
8
by: Goran | last post by:
Hi all, I have a question regarding operator <<. A lib of mine contains a class with an overloaded operator << as NON- class member. This would look like: #include <iostream> #include...
16
by: PeterAPIIT | last post by:
Hello all C++ expert programmer, i have wrote partial general allocator for my container. After reading standard C++ library and code guru article, i have several questions. 1. Why...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...
0
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...

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.