== operation on vectors

S

subramanian100in

In the following program, I have used operator==() as member function
for learning purpose only.

consider the following program:

#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

class Test
{
public:
Test(int arg);
Test(const Test &rhs);
Test& operator=(const Test &rhs);
inline int value() const;
inline bool operator==(const Test &rhs) const;

private:
int val;
};

Test::Test(int arg) : val(arg)
{
}

Test::Test(const Test &rhs) : val(rhs.val)
{
}

Test &Test::eek:perator=(const Test &rhs)
{
if (this != &rhs)
val = rhs.val;

return *this;
}

inline int Test::value() const
{
return val;
}

inline bool Test::eek:perator==(const Test &rhs) const
{
return value() == rhs.value();
}

int main()
{
vector<Test> c1;

c1.push_back(Test(10));

vector<Test> c2;

c2.push_back(Test(10));

if (c1 == c2)
cout << "c1 equals c2" << endl;
else
cout << "not equal" << endl;

return EXIT_SUCCESS;
}

I compiled this program with g++ -std=c++98 -pedantic -Wall -Wextra.

It produced the output
c1 equals c2

However if I remove 'const' from the comparison member function
bool Test::eek:perator==(const Test &rhs) const
ie if I have
bool Test::eek:perator==(const Test &rhs)

Then I get compilation error for the line
if (c1 == c2)

What is the reason for the compilation error when Test::eek:perator==()
is made non-const ?

If I remove operation==() from Test class and make it a free function,
then also the compilation error is gone and the program works fine as
it should be.

Kindly clarify.

Thanks
V.Subramanian
 
J

Juha Nieminen

vector<Test> c1;
c1.push_back(Test(10));
vector<Test> c2;
c2.push_back(Test(10));

if (c1 == c2)
However if I remove 'const' from the comparison member function
bool Test::eek:perator==(const Test &rhs) const
ie if I have
bool Test::eek:perator==(const Test &rhs)

Then I get compilation error for the line
if (c1 == c2)

What is the reason for the compilation error when Test::eek:perator==()
is made non-const ?

std::vector::eek:perator==() is const, and thus cannot call non-const
functions for its elements.
If I remove operation==() from Test class and make it a free function,
then also the compilation error is gone and the program works fine as
it should be.

Because your free function takes both parameters as const references?
If you made it to take non-const references, compilation would probably
fail.
 
J

James Kanze

'inline' means nothing if you don't supply the definition
right after the declaration. Besides, 'inline' is *implicit*
if you *define* the functions in the class definition. So you
can safely remove the 'inline' from the two declarations
above.

That's not right. It means exactly the same thing that it means
if you supply it later. There was once a compiler (an early
version of CFront, I think, but it was so long ago, I can't be
sure) that did require the inline on the first declaration of
the function. But the most widespread current practice is
certainly to only put it before the actual definition of the
function. (That way, you can change from inline to not inline
without touching the class definition.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top