Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-03-14
3
Medium Priority
?
623 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 500 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

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…
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 how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

704 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