Boost logo

Boost :

From: Robert Ramey (ramey_at_[hidden])
Date: 2003-10-22 15:22:23


I have the following class

class archive_exception : public virtual std::exception
{
public:
    typedef enum {
         ....
    } exception_code;
    exception_code code;
    archive_exception(exception_code c) : code(c)
    {}
    virtual const char *what( ) const throw( )
    {
        ...
    }
};

class xml_archive_exception : public virtual archive_exception
{
public:
    typedef enum {
       parse_error
    } xml_exception_code;
    xml_archive_exception(xml_exception_code c)
    {}
    virtual const char *what( ) const throw( )
    {
        ...
    }
};

My problem is when I say

try {
        ....
}
catch(archive_exception e){
        ...
}

throw(xml_archive_exception(xml_archive_exception::parse_error e)

The problem is that e gets sliced so the virtual function will fail

If I change to

catch(archive_exception::parse_error & e)

I would avoid the slicing. but its not clear to me what will happen when I invoke

throw(xml_archive(parse_error))

in that I'm passing by reference an object that is temporarily on the stack.
GCC objects ( rightly in my view) when I do this for normal function calls.

Strousstrup has a lengthy example using a exception class derivation like the above
and I used it. Now I have doubts.

Can anyone shed some light on this?

Robert Ramey


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk