Solved

Declaring member variable of next element in a table

Posted on 1998-09-07
7
242 Views
Last Modified: 2010-04-10
When declaring a function that works on a table of class instances, how can I declare a variable e.g. double length
from the next element in the table, so that when the function is working on the current element in the table, it can access the value of a member variable in the next element. For example, adding the lengths of array[1] and array[i+1]for up to 10 elements.
0
Comment
Question by:Lucret
7 Comments
 
LVL 2

Expert Comment

by:tdubroff
ID: 1172126
Hmm I'm not quite sure what you are trying to accomplish.  If you are saying that you have an array of objects, then create a function that takes as a parameter the array as well as the appropriate index.  Then if you need to access another element in the array, just switch indices.  Did that make any sense for you?
0
 
LVL 1

Author Comment

by:Lucret
ID: 1172127
Hello tdubroff,

Could you give a quick example of the syntax?

0
 
LVL 5

Expert Comment

by:yonat
ID: 1172128
Just pass a pointer to the current element. By increasing this pointer, the function can access the next element.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 1

Expert Comment

by:newexpert
ID: 1172129
If the function can access the member variable of the current element it should be able to access member variable of any element simultaneously.

Say you declare
Class element
 {
  double length;
public:
  double total_length(int number_of_element);
 };

double element::total_length(int number_of_element)
 {
  double sum_length = 0;
  for (int i = 0; i < number_of_element; i++)
   sum_length += this[i].length;
  return sum_length;
 }

But be careful - you must make sure there are number_of_element elements in an array.
0
 
LVL 1

Expert Comment

by:newexpert
ID: 1172130
If the function can access the member variable of the current element it should be able to access member variable of any element simultaneously.

Say you declare
Class element
 {
  double length;
public:
  double total_length(int number_of_element);
 };

double element::total_length(int number_of_element)
 {
  double sum_length = 0;
  for (int i = 0; i < number_of_element; i++)
   sum_length += this[i].length;
  return sum_length;
 }

void main(void)
 {
  element array[5];
  // ...
  // Print out the total length.
  cout << array[0].total_length(5) << end;
 }

0
 
LVL 1

Author Comment

by:Lucret
ID: 1172131
 
0
 
LVL 1

Accepted Solution

by:
Bonev earned 30 total points
ID: 1172132
Suppose you have class CItem with a public member variable double m_nLength.
// Array declaration:
CItem Table[10];

// Use:
double nTotalLength = 0;
for( int i = 0; i < 10; i++ )
  nTotalLength += Table[i].m_nLength;

0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now