T
Taras_96
Hi everyone,
I can't figure out why g++ complains about this code. I gave it to a
programmer friend and he couldn't work it out either. It's quite
simple code, so it's puzzling me. The error I get is:
==========================================
copy_constructor.cpp: In copy constructor `MyCounter::MyCounter(const
MyCounter&)':
copy_constructor.cpp:23: error: passing `const MyCounter' as `this'
argument of `const int MyCounter::getValue()' discards qualifiers
The code is:
==========================================
#include <iostream>
using namespace std;
class MyCounter
{
public:
MyCounter();
~MyCounter();
MyCounter(const MyCounter &);
const int getValue() {return itsVal;}
private:
int itsVal;
};
MyCounter::MyCounter():
itsVal(0)
{
}
MyCounter::MyCounter(const MyCounter & toCopy)
{
itsVal = toCopy.getValue();
}
MyCounter::~MyCounter()
{
}
===================================
If I write 'toCopy.itsValue' instead, the code compiles
Any help would be appreciated
Thanks
Taras
I can't figure out why g++ complains about this code. I gave it to a
programmer friend and he couldn't work it out either. It's quite
simple code, so it's puzzling me. The error I get is:
==========================================
copy_constructor.cpp: In copy constructor `MyCounter::MyCounter(const
MyCounter&)':
copy_constructor.cpp:23: error: passing `const MyCounter' as `this'
argument of `const int MyCounter::getValue()' discards qualifiers
The code is:
==========================================
#include <iostream>
using namespace std;
class MyCounter
{
public:
MyCounter();
~MyCounter();
MyCounter(const MyCounter &);
const int getValue() {return itsVal;}
private:
int itsVal;
};
MyCounter::MyCounter():
itsVal(0)
{
}
MyCounter::MyCounter(const MyCounter & toCopy)
{
itsVal = toCopy.getValue();
}
MyCounter::~MyCounter()
{
}
===================================
If I write 'toCopy.itsValue' instead, the code compiles
Any help would be appreciated
Thanks
Taras