Good afternoon all ( at least from where I live :P )
Working today, when implementing a static methods to calculate an Identity matrix of order "n", I had a doubt about how C++ handles static methods and variables in compilation time, especially because I want to avoid memory leakage.
The code is as follows :
1 2 3 4 5 6 7 8 9 10
const Matrix& Matrix::Identity(int n)
{
static Matrix Temp;
Temp.Resize(n,n)
//do the stuff it needs to be done ...
return Temp;
}
Knowing that this method is static inside the class "Matrix", the variable "Temp" and my method "Identity" would be created only 1 time in compilation time, thus, in execution time, sucessives instances of the class matrix wouldn't create new variables or methods? Or each call of this static method would allocate a new method, thus creating a new variable as well?
There are no static methods, afaik. I don't think you can apply static to a member function or a standalone function. (I could well be wrong but I seriously doubt there are static functions like there are in java. If there are, somebody please correct me.)
But there are static variables. So for each instance of the class, you'd have one instance of that function and one instance of that Temp.
static can be applied to functions. A static free function is one which has only file scope.
A static function declared in a class in one which has no "this" pointer.
Identity() only ever operates on a single "global" matrix instance. It returns a const
reference to it, so unless the caller of the function makes a copy of the return value,
it will magically change the next time Identity is called.
There are. A static method doesn't need the creation of a class to be used, but it can't use methods of an instantiated class nor its the auto reference "this". In other words, it works pretty much like a function in C.
So the above code doesn't have any leakage, right?
Edit: Ok, now I have another doubt
If the method is not static but there is a static variable inside, does each creation of a class instantiate a new variable that won't change after sucessives calls to the class's method or an unique variable will be created that is going to be used in all calls to that method no matter what instance is doing it?
Each instance of the function will have its own copy of the static variable bound to it.
Wow, I had no idea that methods could be rendered static in C++...
Each instance of the function will have its own copy of the static variable bound to it.
well... each time u call this function it works with the same static variable, because the variable will only be created once, and remains, since the statical attribute prevents it from being deleted while the function ends...
so if u have any data in your Temp, it will be still there the next time u call the func... (except from when u changed it - so the changes remain :P)...
tummychow wrote:
Wow, I had no idea that methods could be rendered static in C++...
a static member function can only access static members of the class..., but you could pass a pointer to one of the objects you created to it...
The second question was whether or not a non-static method with a static var created in it would have multiple copies of the static var or not. Because the method is not static, each class object that is instantiated would have a copy of the function, and its own version of that static variable. That's what I was saying.
class Matrix {
staticint x; // static data member x
public:
staticvoid f() { x = 15; } // only static member function can modify static data member x
void Identity(int n) {
static Matrix Temp; // local static object inside Matrix::Identity() function
Temp.Resize(n,n);
//..
}
};
int Matrix::x = 5; //definition of static data member x, usually before main()
//one copy shared among all Matrix objects
//the local static object Temp comes into existence somewhere before main()
//because it is a local static object inside the Matrix::Identity() function
Inside the main() {
Matrix m1;
m1.Identity(10); // apply Resize(10,10) to the single, static object Temp;
Matrix m2;
m2.Identity(20); // apply Resize(20,20) to the single, static object Temp;
Matrix m3;
m3.Identity(30); // apply Resize(30,30) to the single, static object Temp;
}
Correct me if I am wrong in the above explanation.
You might simply want this:
1 2 3 4 5 6 7 8 9 10 11
class Matrix {
public:
void Identity(int n) {
Resize(n,n);
// other ops
}
Resize(int i, int ii) {
// resize ops
}
};
Well, making a little testing I've discovered that the static local object used inside a method is unique among all instances of the object.
Now, I'm wondering, is it a good programming practice to use a static object in my case, that is, to create an object whose function is to return a const reference to itself?
I'm trying to avoid allocating memory on the heap to avoid memory leaks and speeding up the function's processing by eliminating the need to use the copy constructor on the return