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

M

mike

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
 
B

Ben Pope

mike said:
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
 
M

mike

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
 
B

Ben Pope

mike said:
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
 
M

mike

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
 
B

Ben Pope

mike said:
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
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top