473,320 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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_wheels( const car &new_wheels)

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

Thanks.

Nov 7 '06 #1
7 10465
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_wheels( 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_wheels( 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_wheels( 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_wheels() 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******@hotmail.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_wheels( 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 misunderstanding 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******@hotmail.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
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. ...
5
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...
4
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...
7
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...
8
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,...
8
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...
6
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...
12
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...
12
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
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.