473,794 Members | 2,754 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.numDimensio ns() == 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 1308
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.numDimensio ns() == 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
2038
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 possibility of overload these operators not at the global level namespace but into different program namespaces. For instances: #include<iostream> using namespace std;
5
1283
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) superclass's operators. How can that be when operators are always static? Is it a special case? It's handy, sure, but doesn't it "break" the language somehow? Or isn't this actually inheritance at play here?
9
2554
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 anyone using it in his/her code. Any ideas? Thanks, Ben
5
2657
by: puzzlecracker | last post by:
I don't recall whether operators, which are members of the class, are intherited in subclasses? thanks
0
9671
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9518
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10212
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10161
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9035
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2919
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.