nonmember function

Discussion in 'C++' started by bluekite2000@gmail.com, Aug 1, 2005.

  1. Guest

    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, 2005
    #1
    1. Advertising

  2. 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<double>(3, 4);

    V
    Victor Bazarov, Aug 1, 2005
    #2
    1. Advertising

  3. benben Guest

    > 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.
    benben, Aug 3, 2005
    #3
  4. msalters Guest

    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
    msalters, Aug 3, 2005
    #4
    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. Erik Haugen

    nonmember vs member?

    Erik Haugen, Oct 24, 2003, in forum: C++
    Replies:
    13
    Views:
    619
    jeffc
    Oct 24, 2003
  2. James Vanns
    Replies:
    7
    Views:
    7,010
    Evan Carew
    Jan 21, 2004
  3. Replies:
    1
    Views:
    265
    Victor Bazarov
    Jul 29, 2005
  4. Replies:
    2
    Views:
    268
  5. desktop

    nonmember functions?

    desktop, May 23, 2007, in forum: C++
    Replies:
    8
    Views:
    374
    Gennaro Prota
    May 25, 2007
Loading...

Share This Page