473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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
May 17 '06 #1
1 1292
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

May 17 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: GianGuz | last post by:
Global new and delete operators can be overloaded to suite particulars needs. Typically they are overloaded to insert useful debugging/trace informations. What I would to discuss here concerns the...
5
by: Shak | last post by:
Hi all. I was led to believe that static methods were not inherited by their subclasses (and since that makes sense, rightly so). However, a subclass I've written is using it's (abstract)...
9
by: =?Utf-8?B?QmVu?= | last post by:
Hi, I'm trying to figure out the purpose of these operators. Now, I know what they do. They shift bits either left or right. But I don't know why would anybody want to do that. I've never seen...
5
by: puzzlecracker | last post by:
I don't recall whether operators, which are members of the class, are intherited in subclasses? thanks
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.