Solved

Declaring member variable of next element in a table

Posted on 1998-09-07
7
252 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

820 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