How to forbidden using object to static member function

I have a simple class, and a static member function:

class Matrix
{
public: 
    static Matrix returnSomething ( Matrix &m )
    {
        return Matrix(2,2);
    }
};

main function:

int main()
{
    Matrix matrix(2,2);                           // some matrix
    Matrix m = Matrix::returnSomething ( matrix ) // I should use it that way
    m.print() // it shows the matrix       

    // but I can use it too that way //

    Matrix m;
    m.returnSomething ( matrix )                  // how to make this not allowed??
    m.print() // but here the matrix is NULL, wont show anything 
}

How to do it?

Edit:

I have added some print function that shows the problem

Answers


Why not just use a helper class?

class MatrixHelper
{
public: 
    static Matrix returnSomething ( Matrix &m )
    {
        return Matrix(2,2);
    }
};

Then the invocation would be:

MatrixHelper::returnSomething ( matrix ) 

It shouldn't make a difference. However, you can use a namespace instead of static functions

namespace MatrixUtils
{
  Matrix returnSomething ( Matrix &m )
  {
     return Matrix(2,2);
  }
}

You're confusing two issues. The return value "disappears" because you don't assign it to anything - this has no relation to how you invoke the function.

In other words, these will both work:

Matrix matrix(2, 2);
Matrix m = Matrix::returnSomething(matrix);
Matrix m2 = m.returnSomething(martix);

While these will both make the return value "disappear:"

Matrix matrix(2, 2);
Matrix::returnSomething(matrix);
matrix.returnSomething(martix);

Need Your Help

How do I property dissect/disseminate a Dictionary with an Array?

swift nsarray nsdictionary

I'm trying to disseminate the response of a NSJSONSerialization:

Javascript chart inside a jQuery togglable tab

javascript jquery jquery-plugins tabs

I am trying to display a javascript chart inside of a jQuery togglable tab (using Ruby on Rails and bootstrap-tab.js). The chart displays fine if it is outside of the tab, but does not display ins...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.