about static function
-
I'm not sure why you'd make a global function static ( I only glanced at Michael's answer, but whatever he says is most certainly correct. ). A static function loses one of the characteristics of a normal function, that it must be called from a this pointer. In other words, if I have a class called CGraus, and I add a function called EmptyWallet, the I would need to call it from within my class, or through an instance of my class. A static function is like a static variable, it only exists once. A less frivolous xe examples exists in GDI+. The Bitmap class has a static function called FromFile. This means instead of
Bitmap bm;
bm.FromFile("Claudia Schiffer.jpg");I do it like this:
Bitmap * bm = Bitmap::FromFile("Michelle Pfeiffer.jpg");
You'll notice when I call the function there isn't necessarily an instance of Bitmap in memory, certainly the constructor for my pointer has not been called, and is in fact called by my method. For this reason, I suspect the most useful way to use static functions is for methods to initialise pointers of the class in question. I hope I've not muddied the waters too much. ;-) Christian After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001
To answer your doubt about globally static functions. Basically this goes back to good old C, where there are only two types of functions, global and static. The syntactic difference is that static functions have the word "static" in their definition and global ones do not. Static functions are visible only inside the file in which they're compiled. Global functions are visible among all the files in the project. So if you have files A.c and B.c, they can each have a static function with the same name and the linker will treat them as two different functions. But if you remove the static modifier from them, the linker will see the same (global) function defined multiple times and give you an error. So, basically, in C you'd want to make a function "static" when it's only going to be used inside the file it's defined and you want to avoid possible name collisions. Regards, Alvaro
-
To answer your doubt about globally static functions. Basically this goes back to good old C, where there are only two types of functions, global and static. The syntactic difference is that static functions have the word "static" in their definition and global ones do not. Static functions are visible only inside the file in which they're compiled. Global functions are visible among all the files in the project. So if you have files A.c and B.c, they can each have a static function with the same name and the linker will treat them as two different functions. But if you remove the static modifier from them, the linker will see the same (global) function defined multiple times and give you an error. So, basically, in C you'd want to make a function "static" when it's only going to be used inside the file it's defined and you want to avoid possible name collisions. Regards, Alvaro
Cool - thanks for the reply, that expands on what Michael said and I understand how it works now. Christian After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001
-
When
static
is applied to a global function it means the function is visible only in that .CPP file. --Mike-- http://home.inreach.com/mdunn/ #include "witty_sig.h" :love: your :bob: with :vegemite: and :beer: -
I'm not sure why you'd make a global function static ( I only glanced at Michael's answer, but whatever he says is most certainly correct. ). A static function loses one of the characteristics of a normal function, that it must be called from a this pointer. In other words, if I have a class called CGraus, and I add a function called EmptyWallet, the I would need to call it from within my class, or through an instance of my class. A static function is like a static variable, it only exists once. A less frivolous xe examples exists in GDI+. The Bitmap class has a static function called FromFile. This means instead of
Bitmap bm;
bm.FromFile("Claudia Schiffer.jpg");I do it like this:
Bitmap * bm = Bitmap::FromFile("Michelle Pfeiffer.jpg");
You'll notice when I call the function there isn't necessarily an instance of Bitmap in memory, certainly the constructor for my pointer has not been called, and is in fact called by my method. For this reason, I suspect the most useful way to use static functions is for methods to initialise pointers of the class in question. I hope I've not muddied the waters too much. ;-) Christian After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001
- Yep, Christian pal, I use the static function in a class and not used as a global function. - I still have a question. In your reply, you mentioned, "For this reason, I suspect the most useful way to use static functions is for methods to initialise pointers of the class in question." - Since I am a newbie, I do not understand your meaning. - Can you give me an example to show the most useful way to use static functions as you mentioned? - Have a nice day, Maer
-
- Yep, Christian pal, I use the static function in a class and not used as a global function. - I still have a question. In your reply, you mentioned, "For this reason, I suspect the most useful way to use static functions is for methods to initialise pointers of the class in question." - Since I am a newbie, I do not understand your meaning. - Can you give me an example to show the most useful way to use static functions as you mentioned? - Have a nice day, Maer
"For this reason, I suspect the most useful way to use static functions is for methods to initialise pointers of the class in question." - Since I am a newbie, I do not understand your meaning. - Can you give me an example to show the most useful way to use static functions as you mentioned? Probably no example better than the one I gave. In GDI+ the Bitmap class has a method called FromFile and it's static, which means I can start with a Bitmap *, which has not had new called on it yet, and then call the static method, which is a bit like a global function which returns a Bitmap * when given a filename, but being a static function it exists in the namespace of the class. So given that you'll only have one instance of the function AND the function does not require you to have created an instance of the class, a sensible use for them would be to call a function that creates an instance of the class and returns it. I'm sure there are other uses, I use a static function to do a reference count for example. Christian After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001
-
To answer your doubt about globally static functions. Basically this goes back to good old C, where there are only two types of functions, global and static. The syntactic difference is that static functions have the word "static" in their definition and global ones do not. Static functions are visible only inside the file in which they're compiled. Global functions are visible among all the files in the project. So if you have files A.c and B.c, they can each have a static function with the same name and the linker will treat them as two different functions. But if you remove the static modifier from them, the linker will see the same (global) function defined multiple times and give you an error. So, basically, in C you'd want to make a function "static" when it's only going to be used inside the file it's defined and you want to avoid possible name collisions. Regards, Alvaro
- Thanks, Alvaro pal! - Your reply helps a lot. I still have a question. I think the functionality of "static" changes in C++, it means a function that can be called without a pointer. - Since I am a newbie, I do not know whether I am correct. - Yep, still a trouble. Can the function of "static" in the oldie and goldie C still be used in C++? Are they the same? - Can you help? - Have a nice day, Maer
-
"For this reason, I suspect the most useful way to use static functions is for methods to initialise pointers of the class in question." - Since I am a newbie, I do not understand your meaning. - Can you give me an example to show the most useful way to use static functions as you mentioned? Probably no example better than the one I gave. In GDI+ the Bitmap class has a method called FromFile and it's static, which means I can start with a Bitmap *, which has not had new called on it yet, and then call the static method, which is a bit like a global function which returns a Bitmap * when given a filename, but being a static function it exists in the namespace of the class. So given that you'll only have one instance of the function AND the function does not require you to have created an instance of the class, a sensible use for them would be to call a function that creates an instance of the class and returns it. I'm sure there are other uses, I use a static function to do a reference count for example. Christian After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001
- Thanks, Christian pal! - Your reply clarifies my doubts. And I still have a stupid question. - For example, I use a static function named F1 in class A. Can I use the function F1 in another class B without creating an object (or pointer) of class A in class B. ( Suppose class B has no derivation relationship with class A) - Can you help? - Regards, Maer
-
- Thanks, Christian pal! - Your reply clarifies my doubts. And I still have a stupid question. - For example, I use a static function named F1 in class A. Can I use the function F1 in another class B without creating an object (or pointer) of class A in class B. ( Suppose class B has no derivation relationship with class A) - Can you help? - Regards, Maer
In that case it becomes a question of the relationship between A and B. If the function is public or B is a friend of A then that would not be a problem. Christian After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001
-
I'm not sure why you'd make a global function static ( I only glanced at Michael's answer, but whatever he says is most certainly correct. ). A static function loses one of the characteristics of a normal function, that it must be called from a this pointer. In other words, if I have a class called CGraus, and I add a function called EmptyWallet, the I would need to call it from within my class, or through an instance of my class. A static function is like a static variable, it only exists once. A less frivolous xe examples exists in GDI+. The Bitmap class has a static function called FromFile. This means instead of
Bitmap bm;
bm.FromFile("Claudia Schiffer.jpg");I do it like this:
Bitmap * bm = Bitmap::FromFile("Michelle Pfeiffer.jpg");
You'll notice when I call the function there isn't necessarily an instance of Bitmap in memory, certainly the constructor for my pointer has not been called, and is in fact called by my method. For this reason, I suspect the most useful way to use static functions is for methods to initialise pointers of the class in question. I hope I've not muddied the waters too much. ;-) Christian After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001
-
In that case it becomes a question of the relationship between A and B. If the function is public or B is a friend of A then that would not be a problem. Christian After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001
-
- Thanks, Alvaro pal! - Your reply helps a lot. I still have a question. I think the functionality of "static" changes in C++, it means a function that can be called without a pointer. - Since I am a newbie, I do not know whether I am correct. - Yep, still a trouble. Can the function of "static" in the oldie and goldie C still be used in C++? Are they the same? - Can you help? - Have a nice day, Maer
- You're welcome. - In C++ "static" can also be used for class/struct member functions. This allows you to call it without having an instance of the class/struct. So if you have this:
class A
{
public: static void foo();
};You can then call it directly, like this:
A::foo();
without needing to first create an instance of A. - Static C functions work the same way in C++. They're only local to the file in which they're compiled. So if you have a static C function, only that C file can see it. - Hope to have helped, but you really need to pick up a couple of C/C++ books and read them thoroughly. - Regards, Alvaro
-
- You're welcome. - In C++ "static" can also be used for class/struct member functions. This allows you to call it without having an instance of the class/struct. So if you have this:
class A
{
public: static void foo();
};You can then call it directly, like this:
A::foo();
without needing to first create an instance of A. - Static C functions work the same way in C++. They're only local to the file in which they're compiled. So if you have a static C function, only that C file can see it. - Hope to have helped, but you really need to pick up a couple of C/C++ books and read them thoroughly. - Regards, Alvaro
-
- Sorry for interrupting again pal! - I can not find the "FromFile" function. (MSDN-->CBitMap-->class menbers-->) - Can you help? - Regards, Maer
It's a GDI+ function in the Bitmap class, not a GDI function in the CBItmap class. If you don't have the SDK installed, you won't have access to it, but it's covered in the MSDN. Christian After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001