Hi Axel - Thanks for the quick reply. Here's the problem: The intention is that SqMatrix is nothing more than a Matrix with the property that rows = cols. The class declaration class SqMatrix : public Matrix {...} reflects this. So, for example, operator * works in exactly the same way for SqMatrix as for Matrix, and there should be no need to duplicate the code. Meanwhile, Matrix declares operator * by class Matrix { ... Matrix operator * (const Matrix &) const ; } Your solution has extra potential overhead due to the guts-copying of the Matrix into the new SqMatrix. This could be overcome by using smart_ptr or smart_array types, but that all shouldn't be necessary. What is wanted is a cast of a base class object (the returned Matrix) to a reference to derived class object (to a SqMatrix). However gcc (but not msvc++) rejects it. I saw Ian's reply, but all it said was "you can't do that". That's not very helpful - this code has been working for over a decade, so one would hope that the "dotting i's & crossing t's" of increased language rigor would at least leave room for a syntactic workaround giving the same (valid) semantic result. Axel Freyn wrote: > > Hi Oren, > On Wed, May 18, 2011 at 07:36:03AM -0700, Foster Boondoggle wrote: >> >> Hi Adam99 - Did you ever get a reply to this? I have the exact same >> problem >> (indeed, with SqMatrix and Matrix, though I had though I wrote the class >> many years ago!) I can't seem to get it to compile in any form with the >> latest gcc. > > Yes, that question was answered by Ian, see e.g. > http://old.nabble.com/Re%3A-invalid-cast-of-an-rvalue-expression-p23106343.html > The point is: It is illegal C++ :-) > Old compilers (like g++ 4.1.2) accepted such constructs, but the new > gcc-versions detect it as failure > > In addition: what this line does, is the following: > - take "this", a pointer to a "const SqMatrix" > - convert it to a Pointer to a "Matrix" (already problematic: you > remove the const-ness here...) > Yes, that's old-style C++, but equivalent to a const_cast. > > - take the element and multiply it with "sm" > - convert the pointer to a constant reference to a SqMatrix > This used to be interpreted by the compiler as just an instruction to > reinterpret the object as being of the derived type. > - create a copy of this reference and return it. > How about, just return the object but understand it as being of the > derived type. > > that sounds a bit complex to me. > > In order to propose a solution, we would need more information (e.g. how > are the classes defined? is SqMatrix derived from Matrix? are there any > Conversion-Operators / Copy-Constructors defined which can be used for > this conversions? > The easiest solution for this code segment would be to add a new > copy-constructor to SqMatrix: > class SqMatrix { > public: > explicit SqMatrix(const Matrix &m) : ...{ > assert(m.Rows() == m.Columns()); > .... > } > }; > which verifies that m is really square, and then creates a SqMatrix from > m (I added "explicit" in order to block implicit type-conversions Matrix > -> SqMatrix -- this constructor has to be called directly). > > With that, you could rewrite your code-segment as > > SqMatrix operator * (const SqMatrix &sm) const { > return SqMatrix(*(const Matrix *)this * sm); } > > which would be save (the type-conversion is cleanly done) and portable. > (however, as I don't know the full code, may be this solution poses > other problems?) > > Axel (I'm not adam99 ;-)) > > ... > > -- View this message in context: http://old.nabble.com/invalid-cast-of-an-rvalue-expression-tp23105605p31648660.html Sent from the gcc - Help mailing list archive at Nabble.com.