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

Discussion in 'C++' started by Dave Rudolf, May 17, 2006.

  1. Dave Rudolf

    Dave Rudolf Guest

    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::eek: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
    Dave Rudolf, May 17, 2006
    #1
    1. Advertising

  2. Dave Rudolf

    Kai-Uwe Bux Guest

    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::eek:perator=


    NumericVector::eek:perator=( 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
    Kai-Uwe Bux, May 17, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Asfand Yar Qazi
    Replies:
    4
    Views:
    396
    Asfand Yar Qazi
    Nov 12, 2004
  2. puzzlecracker

    can operators be inherited?

    puzzlecracker, Aug 1, 2007, in forum: C++
    Replies:
    5
    Views:
    288
    James Kanze
    Aug 2, 2007
  3. Logan Capaldo
    Replies:
    6
    Views:
    141
    Robert Klemme
    May 6, 2005
  4. 7stud --
    Replies:
    11
    Views:
    405
    7stud --
    Nov 9, 2007
  5. Stephen Howe
    Replies:
    3
    Views:
    243
    Öö Tiib
    Dec 2, 2012
Loading...

Share This Page