|
From: Ray K. <kul...@wr...> - 2005-02-26 00:50:40
|
On Fri, 25 Feb 2005 15:01:15 -0800, Farzan Hajian wrote:
> Hi,
> I have two different questions:
> -------------------------------------------
> The First:
> What is a static function? For example what is the difference
> between the following function and its non-static version??
> //Static Version:
> Static int MyFunc(int n)
> {
> return n*2;
> }
> //Non-Static Version:
> int MyFunc(int n)
> {
> return n*2;
> }
"static" for a function means the same thing as "const" for a
variable. If you declare a function static, the compiler won't
let you give separate definitions for the function in
different source files.
By the way, C++ is case sensitive, so you need to type
"static" instead of "Static". Otherwise, you might get some
points taken off for this question.
> The Second:
> How can I overload the "<<" operator?
> Thanks,
You didn't mention what datatype you wanted to overload it
for, so I used a char*. It should be easy enough to convert
this method to use another datatype.
/*****************************************************/
char *overload(std::ostream &out, char *p) {
return (char*)((unsigned)p << (unsigned)&out);
}
cout << overload(cout, "cheater");
/*****************************************************/
From now on, though, you should probably do at least some of
your homework yourself. I won't always have enough time to
help you.
|