Solved

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

Posted on 2011-03-14
3
597 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
[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
  • 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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
FMX enumerated colours 2 157
c++ substatte a varabe for a string in a LPCTSTR statment 8 101
C++ mouse_event mouse look 7 136
Gaming Software 1 39
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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

710 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