valarray subclass value assignment

M

Michael Hopkins

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::eek:perator=( const T & )

to assign all values to the supplied T using valarray's operator

valarray& valarray::eek:perator=( 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>::eek:perator=( 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 - please CC to my email

Michael


_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/

_/ _/ _/_/_/ Hopkins Research Ltd
_/ _/ _/ _/
_/_/_/_/ _/_/_/ http://www.hopkins-research.com/
_/ _/ _/ _/
_/ _/ _/ _/ 'touch the future'

_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
 
R

Rolf Magnus

Michael said:
But neither (1) nor (2) below work and I'm not sure why.

uo_val& operator=( const T & val ) { // (1)
return std::valarray<T>::eek:perator=( val );
}

Refuses to compile with:
error: invalid initialization of reference of type 'hr::uo_val<double>&'
from expression of type 'std::valarray<double>'

You can't bind a reference to uo_val to an std::valarray said:
uo_val& operator=( const T & val ) { // (2)
*this = val;
return *this;
}

Compiles but gives runtime error signal 11 (SIGSEGV)

Well, this is a recursion. Your operator= will call itself over and over
again, usually until your stack is exhausted.

How about:

uo_val& operator=( const T & val )
{
std::valarray<T>::eek:perator=( val );
return *this;
}
 
K

Kai-Uwe Bux

Michael said:
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::eek:perator=( const T & )

to assign all values to the supplied T using valarray's operator

valarray& valarray::eek:perator=( 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>::eek:perator=( val );

you are returning the subobject of type std::valarray<T>. Try:

uo_val & operator= ( T const & t ) {
std::valarray<T>::eek:perator= ( t );
return( *this );
}

}

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)

You have an infinite loop. The segfault is most likely a stack overflow.


Best

Kai-Uwe Bux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top