C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

valarray subclass value assignment

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Michael Hopkins
Guest





PostPosted: Sat Oct 29, 2005 1:10 pm    Post subject: valarray subclass value assignment Reply with quote





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





PostPosted: Sat Oct 29, 2005 8:17 pm    Post subject: Re: valarray subclass value assignment Reply with quote



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





PostPosted: Sun Oct 30, 2005 1:21 pm    Post subject: Re: valarray subclass value assignment Reply with quote



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





PostPosted: Sun Oct 30, 2005 1:22 pm    Post subject: Re: valarray subclass value assignment Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.