Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Declaring member variable of next element in a table

Posted on 1998-09-07
7
Medium Priority
?
263 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

660 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