public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Inconsisten error messages on const pointers
@ 2012-10-14 21:34 Arthur Schwarz
  2012-10-14 21:56 ` Jonathan Wakely
  0 siblings, 1 reply; 3+ messages in thread
From: Arthur Schwarz @ 2012-10-14 21:34 UTC (permalink / raw)
  To: gcc-help

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]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Inconsisten error messages on const pointers
  2012-10-14 21:34 Inconsisten error messages on const pointers Arthur Schwarz
@ 2012-10-14 21:56 ` Jonathan Wakely
  2012-10-15  6:51   ` Arthur Schwarz
  0 siblings, 1 reply; 3+ messages in thread
From: Jonathan Wakely @ 2012-10-14 21:56 UTC (permalink / raw)
  To: Arthur Schwarz; +Cc: gcc-help

On 14 October 2012 21:33, Arthur Schwarz wrote:
> 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?

In 1 'cell1' refers to this->cell1 which is a const SlipCellBase*, so
you can't call the non-const dump function

In 2 'cell1' refers to the constructor argument, which has type
SlipCellBase*, so you can call the non-const function.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Inconsisten error messages on const pointers
  2012-10-14 21:56 ` Jonathan Wakely
@ 2012-10-15  6:51   ` Arthur Schwarz
  0 siblings, 0 replies; 3+ messages in thread
From: Arthur Schwarz @ 2012-10-15  6:51 UTC (permalink / raw)
  To: Jonathan Wakely; +Cc: gcc-help

Jonathon;

There is a indentation in my forhead where I said "I knew that".

thanks
art



----- Original Message ----
From: Jonathan Wakely <jwakely.gcc@gmail.com>
To: Arthur Schwarz <aschwarz1309@att.net>
Cc: gcc-help@gcc.gnu.org
Sent: Sun, October 14, 2012 2:20:21 PM
Subject: Re: Inconsisten error messages on const pointers

On 14 October 2012 21:33, Arthur Schwarz wrote:
> 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?

In 1 'cell1' refers to this->cell1 which is a const SlipCellBase*, so
you can't call the non-const dump function

In 2 'cell1' refers to the constructor argument, which has type
SlipCellBase*, so you can call the non-const function.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2012-10-14 21:56 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-10-14 21:34 Inconsisten error messages on const pointers Arthur Schwarz
2012-10-14 21:56 ` Jonathan Wakely
2012-10-15  6:51   ` Arthur Schwarz

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).