Passing a class by reference ??

Discussion in 'C++' started by andy, Nov 7, 2006.

  1. andy

    andy Guest

    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.
    andy, Nov 7, 2006
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Nov 7, 2006
    #2
    1. Advertising

  3. 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 );
    }
    Gianni Mariani, Nov 7, 2006
    #3
  4. andy

    andy Guest

    Ah I see.

    So &new_wheels is a copy of class car and all the objects currently
    contained within it ?
    andy, Nov 7, 2006
    #4
  5. andy

    Salt_Peter Guest

    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;
    }
    Salt_Peter, Nov 7, 2006
    #5
  6. andy

    Daniel T. Guest

    "andy" <> wrote:

    > 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.
    Daniel T., Nov 7, 2006
    #6
  7. andy

    eruhk Guest

    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?
    eruhk, Nov 7, 2006
    #7
  8. andy

    Nate Barney Guest

    Daniel T. wrote:
    > "andy" <> wrote:
    >> 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
    Nate Barney, Nov 7, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charlie@CBFC
    Replies:
    1
    Views:
    1,646
    bruce barker \(sqlwork.com\)
    Apr 13, 2006
  2. E11
    Replies:
    1
    Views:
    4,708
    Thomas Weidenfeller
    Oct 12, 2005
  3. sam pal
    Replies:
    3
    Views:
    525
    E. Robert Tisdale
    Jul 16, 2003
  4. Jeff
    Replies:
    4
    Views:
    547
    Peter_Julian
    Mar 8, 2006
  5. Replies:
    2
    Views:
    129
Loading...

Share This Page