Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Operators, how they are inherited, how they might be optimized (http://www.velocityreviews.com/forums/t454091-operators-how-they-are-inherited-how-they-might-be-optimized.html)

 Dave Rudolf 05-17-2006 06:15 AM

Operators, how they are inherited, how they might be optimized

Hey all,

The best way to explain my problem is with an example, so here goes:

I have a class that I use to represent a numeric vector (i.e., a point in some
multi-dimensional space). The class has operators for assignment, addition,
etc. So, for example, the essentials of the class are

class NumericVector
{
public:
// Simple constructor
NumericVector( int numDimensions )
: _elements( new double[ numDimensions ] ),
_numDimensions( numDimensions )
{
}

// element operator
double& operator[]( int element )
{
return _elements[ element ];
}

// assignment operator
NumericVector& operator=( const NumericVector& vec )
{
for( int i = 0; i != _numDimensions; ++i )
{
_elements[ i ] = vec._elements[ i ];
}
}

private:
int _numDimensions;
double* _elements;

};

Don't worry about the implementation of those member methods too much, other
than the constructor

Suppose I then want to create a subclass that demands that the vector have a
certain number of dimensions to it, like so:

class NumericVector3D : public NumericVector
{
public:
// Simple constructor
NumericVector3D()
: NumericVector( 3 )
{
}

// assignment operator (now needs to enforce that vec is of length 3)
NumericVector& operator=( const NumericVector& vec )
{
assert( vec.numDimensions() == 3 );
// TODO: Somehow invoke the old NumericVector::operator=
}
};

As the TODO comment says, I want to invoke the old operator= from the
NumericVector base class. Is there any way to do that? I could copy-and-paste
the code, but that does introduce a maintenance issue, and I was just curious
if there was another way.

Also, I was wondering about how most compilers optimize the use of the
arithmetic operators. For instance, suppose I define, say, operator+. Then it
is more efficient to do something like

NumericVector v1( 1 );
v1[ 0 ] = 1;

NumericVector v2( 1 );
v2[ 0 ] = 2;

NumericVector sum = v1 + v2;

Now, that last line is inefficient, because the operator+ causes a temporary
object to be created, and then calls the operator= on that temp object.
Supposedly, a better way to do it is like this:

NumericVector sum( v1 );
sum += v2;

My complaint in doing the above is that it makes the code less readable. The
above example is simple enough, but it gets ugly for large equations. SO are
there common compilers that would automatically make the substitution from the
less efficient (but easier to read) code to the more efficient one?

Thanks.

Dave

 Kai-Uwe Bux 05-17-2006 06:58 AM

Re: Operators, how they are inherited, how they might be optimized

Dave Rudolf wrote:

> Hey all,
>
> The best way to explain my problem is with an example, so here goes:
>
> I have a class that I use to represent a numeric vector (i.e., a point in
> some multi-dimensional space). The class has operators for assignment,
> addition, etc. So, for example, the essentials of the class are
>
>
> class NumericVector
> {
> public:
> // Simple constructor
> NumericVector( int numDimensions )
> : _elements( new double[ numDimensions ] ),
> _numDimensions( numDimensions )
> {
> }
>
>
> // element operator
> double& operator[]( int element )
> {
> return _elements[ element ];
> }
>
> // assignment operator
> NumericVector& operator=( const NumericVector& vec )
> {
> for( int i = 0; i != _numDimensions; ++i )
> {
> _elements[ i ] = vec._elements[ i ];
> }
> }
>
> private:
> int _numDimensions;
> double* _elements;
>
> };
>
> Don't worry about the implementation of those member methods too much,
> other than the constructor
>
> Suppose I then want to create a subclass that demands that the vector have
> a certain number of dimensions to it, like so:
>
> class NumericVector3D : public NumericVector
> {
> public:
> // Simple constructor
> NumericVector3D()
> : NumericVector( 3 )
> {
> }
>
> // assignment operator (now needs to enforce that vec is of
> length 3) NumericVector& operator=( const NumericVector& vec )

You mean:

NumericVector3D& operator= ( const NumericVector & vec )

> {
> assert( vec.numDimensions() == 3 );
> // TODO: Somehow invoke the old NumericVector::operator=

NumericVector::operator=( vec );
return ( *this );

> }
> };
>
> As the TODO comment says, I want to invoke the old operator= from the
> NumericVector base class. Is there any way to do that? I could
> copy-and-paste the code, but that does introduce a maintenance issue, and
> I was just curious if there was another way.
>
> Also, I was wondering about how most compilers optimize the use of the
> arithmetic operators. For instance, suppose I define, say, operator+. Then
> it is more efficient to do something like
>
> NumericVector v1( 1 );
> v1[ 0 ] = 1;
>
> NumericVector v2( 1 );
> v2[ 0 ] = 2;
>
> NumericVector sum = v1 + v2;
>
> Now, that last line is inefficient, because the operator+ causes a
> temporary object to be created, and then calls the operator= on that temp
> object.

No, it does not. It invokes the copy constructor on that temporary. The line
above is strictly equivalent to:

NumericVector sum ( v1 + v2 );

In this case, the compiler is very likely to elide the copy constructor call
and construct the temporary directly into the memory for the object sum.

However, if you have

NumericVector sum;
// some code;
sum = v1 + v2;

then operator= is called on a temporary.

> Supposedly, a better way to do it is like this:
>
> NumericVector sum( v1 );
> sum += v2;
>
> My complaint in doing the above is that it makes the code less readable.
> The above example is simple enough, but it gets ugly for large equations.
> SO are there common compilers that would automatically make the
> substitution from the less efficient (but easier to read) code to the more
> efficient one?

Compilers cannot read your intentions. To a compiler, the assumption that
operator+ and operator+= are somehow related is very far fetched. I do not
know of any compiler that would substitute one for the other.

If you worry about temporaries, you might want to google for expression
templates.

You also may want to consider using one of the available linear algebra
libraries.

Best

Kai-Uwe Bux

 All times are GMT. The time now is 01:28 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.