Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > object as a argument of the method in which the method is defined ?

Reply
Thread Tools

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

 
 
mike
Guest
Posts: n/a
 
      02-18-2006
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

 
Reply With Quote
 
 
 
 
Ben Pope
Guest
Posts: n/a
 
      02-18-2006
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...
 
Reply With Quote
 
 
 
 
mike
Guest
Posts: n/a
 
      02-21-2006
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

 
Reply With Quote
 
Ben Pope
Guest
Posts: n/a
 
      02-22-2006
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:stream& operator<<(std: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...
 
Reply With Quote
 
mike
Guest
Posts: n/a
 
      02-22-2006
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

 
Reply With Quote
 
Ben Pope
Guest
Posts: n/a
 
      02-22-2006
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...
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Method default argument whose type is the class not yet defined Jennie Python 24 11-12-2012 04:46 AM
While executing the class definition which object is referenced bythe first argument of the class method, Y r Object attributes not allowed asdefault arguments Krishna Python 4 03-07-2008 09:44 PM
constants defined in Kernel are also defined in Object? Paul Brannan Ruby 13 03-04-2008 03:09 PM
how to determine which file a method is defined in Fenton Travers Ruby 2 08-01-2007 10:40 PM
#if (defined(__STDC__) && !defined(NO_PROTOTYPE)) || defined(__cplusplus) Oodini C Programming 1 09-27-2005 07:58 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57