Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Declaring member variable of next element in a table

Posted on 1998-09-07
7
Medium Priority
?
269 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 90 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

963 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