object as a argument of the method in which the method is defined ?

Discussion in 'C++' started by mike, Feb 18, 2006.

  1. mike

    mike Guest

    hi,

    how can i use the object as the parameter of the method, in the body of
    a class in which the method is defined ?

    example: assume i've got a class imaginary_number, and one of the
    methods is imgaginary_number::add(imaginary_number a, imaginary_number
    b)

    how could this be done ?

    thanks,
    mike
     
    mike, Feb 18, 2006
    #1
    1. Advertising

  2. mike

    Ben Pope Guest

    Re: object as a argument of the method in which the method is defined?

    mike wrote:
    > hi,
    >
    > how can i use the object as the parameter of the method, in the body of
    > a class in which the method is defined ?
    >
    > example: assume i've got a class imaginary_number, and one of the
    > methods is imgaginary_number::add(imaginary_number a, imaginary_number
    > b)
    >
    > how could this be done ?


    You could pass the arguments by constant reference.

    Since the elements are probably public anyway (or have a public getter)
    you can also use a free function:

    imaginary_number add(const imaginary_number& lhs,
    const imaginary_number& rhs)
    {
    // clever stuff returning result
    }

    You could also create a free standing operator+, += etc. etc.

    When deciding whether to use a free function or a member function,
    prefer a free function. Only use a member function when you have to
    (such as requiring knowledge of private members).

    Ben Pope
    --
    I'm not just a number. To many, I'm known as a string...
     
    Ben Pope, Feb 18, 2006
    #2
    1. Advertising

  3. mike

    mike Guest

    thanks, i didn't manage to make it working exactly like you've
    described, but it works like this:

    imaginary_number add(imaginary_number lhs,imaginary_number rhs)
    {
    // clever stuff returning result

    }

    cheers,
    michal
     
    mike, Feb 21, 2006
    #3
  4. mike

    Ben Pope Guest

    Re: object as a argument of the method in which the method is defined?

    mike wrote:
    > thanks, i didn't manage to make it working exactly like you've
    > described, but it works like this:
    >
    > imaginary_number add(imaginary_number lhs,imaginary_number rhs)
    > {
    > // clever stuff returning result
    >
    > }


    I don't see the problem:

    #include <iostream>
    #include <string>

    struct imaginary_number {
    imaginary_number(int x_, int y_ = 0) : x(x_), y(y_) {}
    int x;
    int y;
    };

    imaginary_number add(const imaginary_number& lhs,
    const imaginary_number& rhs) {
    return imaginary_number(lhs.x+rhs.x, lhs.y+rhs.y);
    }

    std::eek:stream& operator<<(std::eek:stream& os, const imaginary_number& im) {
    return os << im.x << ", " << im.y;
    }

    int main() {
    imaginary_number im1 = 4;
    imaginary_number im2(4,3);
    imaginary_number result1(add(im1, im2));
    imaginary_number result2 = add(im1, im2);

    std::cout << "result1: " << result1 << "\n";
    std::cout << "result2: " << result2 << std::endl;
    }


    Ben Pope
    --
    I'm not just a number. To many, I'm known as a string...
     
    Ben Pope, Feb 22, 2006
    #4
  5. mike

    mike Guest

    ok, i get the point, but here you left all the data of imaginary_number
    public (feature of a structure) and i tried to do it via setters and
    getters

    cheers,
    m
     
    mike, Feb 22, 2006
    #5
  6. mike

    Ben Pope Guest

    Re: object as a argument of the method in which the method is defined?

    mike wrote:
    > ok, i get the point, but here you left all the data of imaginary_number
    > public (feature of a structure) and i tried to do it via setters and
    > getters


    That shouldn't pose a problem if you keep yourself const correct:

    class imaginary_number {
    public:
    imaginary_number(int x, int y = 0) : x_(x), y_(y) {}
    int getX() const { return x_; }
    int getY() const { return y_; }
    private:
    int x_;
    int y_;
    };

    imaginary_number add(const imaginary_number& lhs,
    const imaginary_number& rhs) {
    return imaginary_number(lhs.getX()+rhs.getX(),lhs.getY()+rhs.getY());
    }

    **Uncompiled code warning**

    Notice the const at the end of the get function and return by value.
    Without it you cannot pass a const imaginary_number into the function
    and call the getter.

    Ben Pope
    --
    I'm not just a number. To many, I'm known as a string...
     
    Ben Pope, Feb 22, 2006
    #6
    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. Oodini
    Replies:
    1
    Views:
    1,779
    Keith Thompson
    Sep 27, 2005
  2. Krishna
    Replies:
    4
    Views:
    438
    Steven D'Aprano
    Mar 7, 2008
  3. Fenton Travers
    Replies:
    2
    Views:
    86
    George Malamidis
    Aug 1, 2007
  4. Paul Brannan
    Replies:
    13
    Views:
    296
    Paul Brannan
    Mar 4, 2008
  5. Jennie
    Replies:
    24
    Views:
    355
    Steven D'Aprano
    Nov 12, 2012
Loading...

Share This Page