D
Dave Rudolf
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:perator=
}
};
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
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:perator=
}
};
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