By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,421 Members | 1,127 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,421 IT Pros & Developers. It's quick & easy.

Passing a class by reference ??

P: n/a

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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
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

P: n/a
Ah I see.

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

Nov 7 '06 #4

P: n/a

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

P: n/a
"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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.