Solved

Declaring member variable of next element in a table

Posted on 1998-09-07
7
257 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 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

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…
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 clear a vector as well as how to detect empty vectors in C++.
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.

623 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