 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Michael Hopkins Guest
|
Posted: Sat Oct 29, 2005 1:10 pm Post subject: valarray subclass value assignment |
|
|
Hi all
I have a subclass of valarray<T> thus
template <typename T>
class uo_val : public std::valarray<T>
{
public:
uo_val ( ) : std::valarray<T>() {}
uo_val (const int sz ) : std::valarray<T>( sz ) {}
uo_val (const int sz, const T fill ) : std::valarray<T>( fill, sz ) {}
//etc..
};
and I want to define an operator
uo_val& uo_val::operator=( const T & )
to assign all values to the supplied T using valarray's operator
valarray& valarray::operator=( const T & )
But neither (1) nor (2) below work and I'm not sure why.
uo_val& operator=( const T & val ) { // (1)
return std::valarray<T>::operator=( val );
}
Refuses to compile with:
error: invalid initialization of reference of type 'hr::uo_val<double>&'
from expression of type 'std::valarray<double>'
uo_val& operator=( const T & val ) { // (2)
*this = val;
return *this;
}
Compiles but gives runtime error signal 11 (SIGSEGV)
Could someone please enlighten me?
Many thanks
Michael
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
_/ _/ _/_/_/ Hopkins Research Ltd
_/ _/ _/ _/
_/_/_/_/ _/_/_/ http://www.hopkins-research.com/
_/ _/ _/ _/
_/ _/ _/ _/ 'touch the future'
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Carl Barron Guest
|
Posted: Sat Oct 29, 2005 8:17 pm Post subject: Re: valarray subclass value assignment |
|
|
Michael Hopkins <michael.hopkins (AT) hopkins-research (DOT) com> wrote:
| Quote: | template <typename T
class uo_val : public std::valarray
{
public:
uo_val ( ) : std::valarray
uo_val (const int sz ) : std::valarray<T>( sz ) {}
uo_val (const int sz, const T fill ) : std::valarray<T>( fill, sz ) {}
//etc..
};
and I want to define an operator
uo_val& uo_val::operator=( const T & )
to assign all values to the supplied T using valarray's operator
valarray& valarray::operator=( const T & )
But neither (1) nor (2) below work and I'm not sure why.
uo_val& operator=( const T & val ) { // (1)
return std::valarray<T>::operator=( val );
}
Whats wrong with just using std::valarray<T>'s operator providing one |
explcitly for this class???
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Richter Guest
|
Posted: Sun Oct 30, 2005 1:21 pm Post subject: Re: valarray subclass value assignment |
|
|
Michael Hopkins wrote:
| Quote: | I have a subclass of valarray<T> thus
template <typename T
class uo_val : public std::valarray
{
public:
uo_val ( ) : std::valarray
uo_val (const int sz ) : std::valarray<T>( sz ) {}
uo_val (const int sz, const T fill ) : std::valarray<T>( fill, sz ) {}
//etc..
};
and I want to define an operator
uo_val& uo_val::operator=( const T & )
to assign all values to the supplied T using valarray's operator
valarray& valarray::operator=( const T & )
But neither (1) nor (2) below work and I'm not sure why.
uo_val& operator=( const T & val ) { // (1)
return std::valarray<T>::operator=( val );
}
Refuses to compile with:
error: invalid initialization of reference of type 'hr::uo_val<double>&'
from expression of type 'std::valarray<double>'
|
Because you return with the return value of operator= of the valarray
class, which is not of type uo_val.
| Quote: | uo_val& operator=( const T & val ) { // (2)
*this = val;
return *this;
}
Compiles but gives runtime error signal 11 (SIGSEGV)
|
Because this operator calls itself by its first line.
What about the following:
uo_val &operator=(const T &v) {
std::valarray<T>::operator=( v );
/*
** Now assign the members of v to the members of
** *this, whatever copying is required.
*/
// Finally, return the object itself back.
return *this;
}
So long,
Thomas
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gabriel Dos Reis Guest
|
Posted: Sun Oct 30, 2005 1:22 pm Post subject: Re: valarray subclass value assignment |
|
|
Michael Hopkins <michael.hopkins (AT) hopkins-research (DOT) com> writes:
| Quote: | Hi all
I have a subclass of valarray<T> thus
template <typename T
class uo_val : public std::valarray
{
public:
uo_val ( ) : std::valarray
uo_val (const int sz ) : std::valarray<T>( sz ) {}
uo_val (const int sz, const T fill ) : std::valarray<T>( fill, sz ) {}
//etc..
};
and I want to define an operator
uo_val& uo_val::operator=( const T & )
to assign all values to the supplied T using valarray's operator
valarray& valarray::operator=( const T & )
But neither (1) nor (2) below work and I'm not sure why.
uo_val& operator=( const T & val ) { // (1)
return std::valarray<T>::operator=( val );
}
|
When you have
struct B { };
struct D : B { };
there is no implicit conversion from B& to D&, consequently (1) cannot
compile, because the return statement assumes there is such conversion.
| Quote: | Refuses to compile with:
error: invalid initialization of reference of type 'hr::uo_val<double>&'
from expression of type 'std::valarray<double>'
uo_val& operator=( const T & val ) { // (2)
*this = val;
return *this;
|
This is recursive definition, with no guard, therefore you get an
infinite loop.
None of the two problems has anything to do with valarray per se.
| Quote: | }
Compiles but gives runtime error signal 11 (SIGSEGV)
Could someone please enlighten me?
|
Is there a reason why you want to use inheritance instead of containment?
--
Gabriel Dos Reis
[email]gdr (AT) integrable-solutions (DOT) net[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|