Solved

How do you pass a C++ array element by reference?

Posted on 2011-03-14
3
565 Views
Last Modified: 2012-05-11
How do you pass an individual C++ array element by reference?  Is this possible?

Suppose you have the following class and array.  How would you pass an element of that array into some function while changing the actual element of that array as well as the members of that class instance?

class MyClass
{
     public:
          int member1;
          int member2;
          float member3;
};

MyClass myClassArray[ 10 ];

Open in new window

0
Comment
Question by:CucumberBilly
  • 2
3 Comments
 
LVL 40

Accepted Solution

by:
evilrix earned 125 total points
ID: 35131670
You mean something like this?
void foo(MyClass & c)
{
   // modify c
}


MyClass myClassArray[ 10 ];


foo(myClassArray[1]); // modify the 2nd element of the array.

Open in new window

0
 

Author Closing Comment

by:CucumberBilly
ID: 35131855
Thanks, I didn't realize you could use the same reference operator in a function for array elements.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 35131923
A single array element is both semantically and syntactically identical to its scalar counterpart.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
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…
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

759 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

23 Experts available now in Live!

Get 1:1 Help Now