• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 633
  • Last Modified:

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

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
CucumberBilly
Asked:
CucumberBilly
  • 2
1 Solution
 
evilrixSenior Software Engineer (Avast)Commented:
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
 
CucumberBillyAuthor Commented:
Thanks, I didn't realize you could use the same reference operator in a function for array elements.
0
 
evilrixSenior Software Engineer (Avast)Commented:
A single array element is both semantically and syntactically identical to its scalar counterpart.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now