Passing a class by reference ??

A

andy

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.
 
V

Victor Bazarov

andy said:
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
 
G

Gianni Mariani

andy said:
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 );
}
 
A

andy

Ah I see.

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

Salt_Peter

andy said:
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;
}
 
D

Daniel T.

andy said:
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!
 
E

eruhk

andy said:
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?
 
N

Nate Barney

Daniel said:
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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top