473,396 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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

Feb 18 '06 #1
5 1248
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...
Feb 18 '06 #2
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

Feb 21 '06 #3
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::ostream& operator<<(std::ostream& 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...
Feb 22 '06 #4
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

Feb 22 '06 #5
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...
Feb 22 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of...
26
by: yb | last post by:
Hi, Is there a standard for the global 'window' object in browsers? For example, it supports methods such as setInterval and clearInterval, and several others. I know that w3c standardized...
22
by: Cylix | last post by:
I have a 4row x 1col table, I would like to drop all the content of row three. Since Mac IE5.2 does not suppport deleteRow method, I have also try to set the innerHTML=''; but it does not work. ...
2
by: Ralph | last post by:
Hi I don't understand why it's not working: function schedule(imTop){ this.tdImagesTop = imTop; } schedule.prototype.selectEl = function() { alert(this.tdImagesTop);
21
by: phpCodeHead | last post by:
Code which should allow my constructor to accept arguments: <?php class Person { function __construct($name) { $this->name = $name; } function getName()
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.