473,669 Members | 2,372 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing a class by reference ??


A question about about passing a class by reference:

Say you have a class called car, and within that you have two objects
called car01 and car02.

Within the class I have an int variable called wheels.

I have declared the following:

int number_of_wheel s( const car &new_wheels)

Now how do I within this function access the variables 'wheels' of
object car01 ?

Thanks.

Nov 7 '06 #1
7 10491
andy wrote:
A question about about passing a class by reference:
Just to make sure we use proper terminology: there is no "passing
a class", there is "passing an object".
Say you have a class called car, and within that you have two objects
called car01 and car02.

Within the class I have an int variable called wheels.

I have declared the following:

int number_of_wheel s( const car &new_wheels)

Now how do I within this function access the variables 'wheels' of
object car01 ?
Unless 'car01' is global, you can't. You can only access member
variables in 'new_wheels' object.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 7 '06 #2
andy wrote:
A question about about passing a class by reference:

Say you have a class called car, and within that you have two objects
called car01 and car02.

Within the class I have an int variable called wheels.

I have declared the following:

int number_of_wheel s( const car &new_wheels)

Now how do I within this function access the variables 'wheels' of
object car01 ?

class car
{
public:

car()
: wheels(4)
{
}

int wheels;
};
int getwheels( const car & i_car )
{
return i_car.wheels;
}

int main()
{
car mycar;

mycar.wheels = 6;

getwheels( mycar );
}
Nov 7 '06 #3
Ah I see.

So &new_wheels is a copy of class car and all the objects currently
contained within it ?

Nov 7 '06 #4

andy wrote:
A question about about passing a class by reference:

Say you have a class called car, and within that you have two objects
called car01 and car02.

Within the class I have an int variable called wheels.

I have declared the following:

int number_of_wheel s( const car &new_wheels)

Now how do I within this function access the variables 'wheels' of
object car01 ?

Thanks.
By calling a constant accessor.
Did you mean to say that 2 instances of the Car class exist?
Because you make it sound like your Car has 2 cars "in it".
class Car is a type, it doesn't "exist" until you actually make one.

#include <iostream>

class Car
{
int wheels;
public:
Car() : wheels(4) { } // def ctor
Car(int w) : wheels(w) { } // parametized ctor
int number_of_wheel s() const { return wheels; }
};

int main()
{
Car car01; // 4 wheels
std::cout << "car01 has this many wheels: \n";
std::cout << car01.number_of _wheels() << std::endl;
Car car02(5);
std::cout << "car02 has this many wheels: \n";
std::cout << car02.number_of _wheels() << std::endl;
}

Nov 7 '06 #5
"andy" <an******@hotma il.comwrote:
A question about about passing a class by reference:
You don't pass a class by reference, you pass objects by reference.
Say you have a class called car, and within that you have two objects
called car01 and car02.
What type is car01 and car02? If they are cars then they likely
shouldn't be "within" the class.
Within the class I have an int variable called wheels.

I have declared the following:

int number_of_wheel s( const car &new_wheels)
Where did you declare it? Is it within the car class?
Now how do I within this function access the variables 'wheels' of
object car01 ?
You seem to have a fundamental misunderstandin g as to what a "class" is
and what an "object" is. If you go to your kitchen and pick up three
fruits, you will have three objects in your hand, each will be unique.
You would never say they are "within" the concept "fruit", they are
manifestations of that concept. One of the properties of a fruit is it
contains seeds. I.E., the seeds are within the fruit.

I think you should read more about these concepts before you start
coding.

Good luck!

--
To send me email, put "sheltie" in the subject.
Nov 7 '06 #6
andy wrote:
Ah I see.

So &new_wheels is a copy of class car and all the objects currently
contained within it ?
No, it is a REFERENCE to the car it was passed. Consider:

void a (int i) {
++i;
}

void b (int& i) {
++i;
}

int main () {
int i = 0;
a(i);
assert(i == 0);
b(i);
assert(i == 1);
}

"a" accepts a copy of the argument. It won't modify the original, and it
calls the copy constructor of the object to make a unique version. This
takes longer, depending on the type of Object.

"b" accepts a REFERENCE to the argument. It points to the exact same
location in memory, and therefore any modifications made in the function
will affect the original. Make sense?
Nov 7 '06 #7
Daniel T. wrote:
"andy" <an******@hotma il.comwrote:
>Say you have a class called car, and within that you have two objects
called car01 and car02.

What type is car01 and car02? If they are cars then they likely
shouldn't be "within" the class.
In mathematical terms, the word 'class' can be used to mean "a set of
things, most likely with some common properties". OOP terminology
probably uses the word class for similar reasons. In this sense, you
could think of instances of an OOP class to be *in* the class. I
presume the OP meant something along these lines here.
>Within the class I have an int variable called wheels.
Here it's pretty obvious the OP is using the word 'within' to refer to a
member variable.

Andy, you'd probably be better off using the term 'instance' when
talking about objects with a given class type and the phrase 'member
variable' when talking about objects that are part of other objects.
The word 'within' is potentially ambiguous.

Nate
Nov 7 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
4668
by: Dave | last post by:
I'm currently working on a small project (admitedly for my CS class) that compares the time difference between passing by value and passing by reference. I'm passing an array of 50000 int's. However, since in C++ an array is passed by reference by default I need to embed the array into a struct in order to pass it by value. The problem is that I get a segmentation error when doing so. I'm using the Dev-c++ compiler. Any ideas? ...
5
36398
by: Andy | last post by:
Hi Could someone clarify for me the method parameter passing concept? As I understand it, if you pass a variable without the "ref" syntax then it gets passed as a copy. If you pass a variable with the "ref" syntax then it gets passed as a reference to the object and any changes to
4
4865
by: Ron Rohrssen | last post by:
I want to show a dialog and when the form (dialog) is closed, return to the calling form. The calling form should then be able to pass the child form to another object with the form as a parameter. For example, FormOptions formOptions = new FormOptions(); if (formOptions.ShowDialog(this) == DialogResult.OK) {
7
4742
by: Ken Allen | last post by:
I have a .net client/server application using remoting, and I cannot get the custom exception class to pass from the server to the client. The custom exception is derived from ApplicationException and is defined in an assembly common to the client and server components. The custom class merely defines three (3) constructors -- the null constructor; one with a string parameter; and one with a string and innner exception parameter -- that...
8
2114
by: Dennis Myrén | last post by:
I have these tiny classes, implementing an interface through which their method Render ( CosWriter writer ) ; is called. Given a specific context, there are potentially a lot of such objects, each requiring a call to that method to fulfill their purpose. There could be 200, there could be more than 1000. That is a lot of references passed around. It feels heavy. Let us say i changed the signature of the interface method to:
8
4408
by: Johnny | last post by:
I'm a rookie at C# and OO so please don't laugh! I have a form (fclsTaxCalculator) that contains a text box (tboxZipCode) containing a zip code. The user can enter a zip code in the text box and click a button to determine whether the zip code is unique. If the zip code is not unique, another form/dialog is displayed (fclsLookup) - lookup form/dialog. The zip code is passed to the lookup form/dialog by reference. I then load a...
6
5989
by: MSDNAndi | last post by:
Hi, I get the following warning: "Possibly incorrect assignment to local 'oLockObject' which is the argument to a using or lock statement. The Dispose call or unlocking will happen on the original value of the local." My code is: using System; using System.Collections.Generic;
12
3014
by: scottt | last post by:
hi, I am having a little problem passing in reference of my calling class (in my ..exe)into a DLL. Both programs are C# and what I am trying to do is pass a reference to my one class into a DLL function. When I try and compile the DLL I get "The type or namespace name "MyForm" could not be found. I think I have to reference the class but since the DLL needs to be built before the EXE it looks like I have a chicken and egg type problem....
12
2678
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
7
3297
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the object is a reference type? my code is not proving that. I have a web project i created from a web service that is my object: public class ExcelService : SoapHttpClientProtocol {
0
8465
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8894
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8658
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7407
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4384
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2792
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2029
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1787
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.