473,395 Members | 1,377 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,395 software developers and data experts.

Derived member variables.

Do you only add derived member variables to a class to improve performance?
Here is an example:

int square(int integer) { return integer * integer; }

vector<int> compute_squares(vector<int> integers)
{
vector<int> squares;
transform(integers.begin(), integers.end(), back_inserter(squares),
square);
return squares;
}

class MyIntegers
{
vector<int> integers;
vector<int> squares;

public:
MyIntegers(vector<int> integers_) : integers(integers_),
squares(compute_squares(integers)) { }

void scale(int factor)
{
transform(integers.begin(), integers.end(), integers.begin(),
bind2nd(multiplies<int>(), factor)));
transform(squares.begin(), squares.end(), squares.begin(),
bind2nd(multiplies<int>(), factor * factor)));
}

vector<int> get_squares() const { return squares; }
};

Since MyIntegers has a derived member variable called squares, it's member
function get_squares only has to return a value. Therefore if get_squares is
called more than any other function, then the addition of squares to
MyIntegers pays off at run-time. Is this the only reason you would add
derived member variables to a class?
Jul 22 '05 #1
2 1432
* Jason Heyes:

Do you only add derived member variables to a class to improve performance?


Although also this question smells strongly of HOMEWORK, I can't see
what you'd "gain" by copying or paraphrasing a serious answer, so.

Assuming 'derived member variable' means 'a member variable whose value
can be derived from other member variables': no, it can be added for
reasons other than performance.

A variable has functionality that a computed value has not; you might
learn a bit by pondering the question of what that functionality is.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #2
"Alf P. Steinbach" <al***@start.no> wrote in message
news:41*****************@news.individual.net...
* Jason Heyes:

Do you only add derived member variables to a class to improve
performance?


Although also this question smells strongly of HOMEWORK, I can't see
what you'd "gain" by copying or paraphrasing a serious answer, so.

Assuming 'derived member variable' means 'a member variable whose value
can be derived from other member variables': no, it can be added for
reasons other than performance.

A variable has functionality that a computed value has not; you might
learn a bit by pondering the question of what that functionality is.


Are you referring to the constness of a variable as opposed to a computed
value? The computed value can be stored in another variable so the same
functionality is there isn't it? Please help me understand.
Jul 22 '05 #3

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

Similar topics

10
by: Scott Brady Drummonds | last post by:
Hi, everyone, I'm still learning Python as I develop a medium-sized project. From my previous experience with C++, I've burnt into my mind the notion of information hiding. I'm having trouble...
0
by: Achim Domma | last post by:
Hi, I have an ascx control which derives from a base class, which already has members for child controls in the ascx. If I edit the ascx in visual studio 2005, the derived class get's...
17
by: lm401 | last post by:
I'm trying to work with the following idea: class animal: def __init__(self, weight, colour): self.weight = weight self.colour = colour class bird(animal): def __init__(self, wingspan):
114
by: Jonathan Wood | last post by:
I was just wondering what naming convention most of you use for class variables. Underscore, "m_" prefix, camel case, capitalized, etc? Has one style emerged as the most popular? Thanks for...
8
by: David Veeneman | last post by:
Should a member variable be passed to a private method in the same class as a method argument, or should the method simply call the member variable? For years, I have passed member variables to...
5
by: 2b|!2b==? | last post by:
I have a class that contains C structs as member variables. By C structs, I mean they cannot have ctors/dtors because they have C linkage (extern "C"). For eg: MyClass { //Impl private:
13
by: Henri.Chinasque | last post by:
Hi all, I am wondering about thread safety and member variables. If I have such a class: class foo { private float m_floater = 0.0; public void bar(){ m_floater = true; }
17
by: Juha Nieminen | last post by:
As we know, the keyword "inline" is a bit misleading because its meaning has changed in practice. In most modern compilers it has completely lost its meaning of "a hint for the compiler to inline...
4
by: aaragon | last post by:
Hi everyone, I have a linking error when using gcc4.2 and static member variables. The class template definition is something around the following: template<> class Element<L2_t: public...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
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...
0
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...

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.