473,888 Members | 1,499 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 10501
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
4694
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
36429
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
4874
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
4759
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
2119
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
4419
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
6008
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
3035
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
2696
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
3317
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
9961
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
9800
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10439
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
9597
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
7148
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
5817
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4244
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3252
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.