Inconsisten error messages on const pointers
Arthur Schwarz
aschwarz1309@att.net
Sun Oct 14 21:34:00 GMT 2012
In the example below the following two statements seem to be inconsistent:
1: cout << " " << endl << cell1->dump();
2: this->message += cell1->dump();
1: produces the error message
SlipException.h: In member function ‘void slip::SlipException::log()’:
In file included from SlipCell.h:49:0,
from SlipDatum.h:269,
from SlipOp.h:22,
from SlipStringOp.h:12,
from SlipStringOP.cpp:11:
SlipException.h:29:79: error: passing ‘const slip::SlipCellBase’ as ‘this’
argument of ‘virtual std::string slip::SlipCellBase::dump()’ discards qualifiers
With 2: there is no diagnostic message. Any reason why?
===============================================================
[code]
# include <exception>
# include <string>
# include <iostream>
# include <stdlib.h>
# include "SlipDef.h"
# include "SlipErr.h"
# include "SlipCellBase.h"
namespace slip {
class SlipException : public exception {
private:
void log() { cout << "error " << message;
SlipCellBase* castCell1 = const_cast<SlipCellBase*>(cell1);
SlipCellBase* castCell2 = const_cast<SlipCellBase*>(cell2);
if (cell1 != NULL) cout << " " << endl <<
cell1->dump(); // error
if (cell2 != NULL) cout << " " << endl <<
castCell2->dump();
cout << endl << flush;
}
protected:
const SlipCellBase* cell1;
const SlipCellBase* cell2;
string message;
SlipException() { };
public:
SlipException(SlipErr::Error message, SlipCellBase* cell1)
: message(constructMessage(message, "", ""))
, cell1(cell1)
, cell2(NULL) {
this->message += cell1->dump(); // no error
log();
}; // SlipException(SlipErr::Error message, SlipCellBase* cell1)
virtual const char* what() const throw() {
return message.c_str();
}; // virtual const char* what() const throw();
~SlipException() throw() { }
}; // class SlipException
[/code]
More information about the Gcc-help
mailing list