473,785 Members | 2,619 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

nonmember function

I d rather have a Matlab syntax such as:

// create a Matrix of size 3,4 and fills it w/ random values.
Matrix<double> M=rand(3,4)

But since this function changes the state of the object, I ought to
make it a member function.

ie.

Matrix<double> M(3,4);
M.rand();

which I dont really like too much.

What is my best option (while maintaining a matlab-like notation)?

Aug 1 '05 #1
3 1474
bl**********@gm ail.com wrote:
I d rather have a Matlab syntax such as:

// create a Matrix of size 3,4 and fills it w/ random values.
Matrix<double> M=rand(3,4)

But since this function changes the state of the object, I ought to
make it a member function.

ie.

Matrix<double> M(3,4);
M.rand();

which I dont really like too much.

What is my best option (while maintaining a matlab-like notation)?


Add a static function to your 'Matrix' class template and let it create
another instance of 'Matrix'. It's called "a factory method":

template<class T> class Matrix {
...
public:
static Matrix rand(int n, int m) {
Matrix temp(n, m);
... // randomize them
return temp;
}
};

Matrix<double> M = Matrix<double>: :rand(3, 4);

You could of course simply have a non-member function, but it would need
to be a template:

template<class T> Matrix<T> rand_matrix(int n, int m) {
...
}

and then you call it:

Matrix<double> M = rand_matrix<dou ble>(3, 4);

V
Aug 1 '05 #2
> I d rather have a Matlab syntax such as:

// create a Matrix of size 3,4 and fills it w/ random values.
Matrix<double> M=rand(3,4)

But since this function changes the state of the object, I ought to
make it a member function.
Not really. If rand() can alter the object state via public interface then
it should be a non-member function, if not, you can still consider making
rand() a friend of Matrix<>.

ie.

Matrix<double> M(3,4);
M.rand();

which I dont really like too much.
Neither do I.

What is my best option (while maintaining a matlab-like notation)?


Answered above.


Aug 3 '05 #3

bl**********@gm ail.com schreef:
I d rather have a Matlab syntax such as:

// create a Matrix of size 3,4 and fills it w/ random values.
Matrix<double> M=rand(3,4)

But since this function changes the state of the object


No, it doesn't. If you want to "change the state of an object"
there must be an object state before and after". In this case,
there is no Matrix before. Therefore, rand() doesn't change it.
The syntax you have is quite good. However since rand() is a
quitegeneric name, I'd prefer a static Matrix<double>: :rand().
That makes it quite clear:

Matrix<double> M = Matrix<double>: :rand(3,4);

HTH,
Michiel Salters

Aug 3 '05 #4

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

Similar topics

13
2443
by: Erik Haugen | last post by:
From reading gotw#84 (http://www.gotw.ca/gotw/084.htm), I'm convinced that I should try to make functions nonfriend nonmembers when practical, but then I came across this: Bruce Eckel says about operator overloads in Thinking In C++ (2nd Ed - http://www.codeguru.com/cpp/tic/tic0129.shtml) "In general, if it doesn't make any difference, they should be members, to emphasize the association between the operator and its class." That seems...
3
14952
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
5
2848
by: phil_gg04 | last post by:
Dear Javascript Experts, Opera seems to have different ideas about the visibility of Javascript functions than other browsers. For example, if I have this code: if (1==2) { function invisible() { alert("invisible() called"); } }
1
1213
by: bluekite2000 | last post by:
The author's reason is "because its hard to write templates that dont know whether or not an operation is a member for a given type". What does he mean by this? An example is appreciated.
2
7678
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would like mine to run immediately after it. So in the code below, what JS would i need to add to my "myfile.inc" page so that I could guarantee this behavior? <!-- main page --> <html> <head> <script type="text/javascript">
2
1983
by: ma740988 | last post by:
So I'm reading the C++ coding standards(Shutter & Andrei), more specifically item 56. There's a statement: "Prefer to provide a nonmember swap function in the same namespace as your type when objects of your type have a way to exchange their values more efficiently than via brute-force assignment, such as if they have their own swap or equivalent function. Additionally, consider specializing std::swap for your own template types:" ...
2
12692
by: sushil | last post by:
+1 #include<stdio.h> +2 #include <stdlib.h> +3 typedef struct +4 { +5 unsigned int PID; +6 unsigned int CID; +7 } T_ID; +8 +9 typedef unsigned int (*T_HANDLER)(void); +10
8
5114
by: Olov Johansson | last post by:
I just found out that JavaScript 1.5 (I tested this with Firefox 1.0.7 and Konqueror 3.5) has support not only for standard function definitions, function expressions (lambdas) and Function constructors (these three I knew about), but also conditional function definitions, as described in http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Defining_Functions ]. An example: function fun() {
8
2160
by: desktop | last post by:
I have read that the allocator class has the following nonmember functions: template <class In, class ForFor uninitialized_copy(In,In,For); template <class For, class T> void uninitialized_fill(For, For, const T&); But what does it mean that the two functions are nonmembers? Are they defined outside the class declaration in the header file?
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9481
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10095
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8978
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7502
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5383
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2881
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.