|
From: Ray K. <kul...@wr...> - 2005-03-01 19:48:08
|
I've received a couple of messages asking about my response to Farzan's
question, so I'd like to clarify:
my answers were meant to be tongue-in-cheek. I was convinced that the
questions had been pulled directly from a homework assignment, and that
Farzan was attempting to cheat, so I attempted to humorously suggest that
he at least attempt to do the work himself before asking us for help.
Both answers I gave are incorrect, and the second code segment I included,
while probably undefined, will do nothing but print "cheater" to stdout
using the version of gcc that comes with Dev-C++ (at least, it did when I
tested it.) My goal was to show that I realized he was attempting to
cheat, and therefore convince him to do the work himself. However, it
would appear that sarcasm is not really my forte, so I'll probably just
stick to a serious response, as I did the last time someone posted a
homework question here, from now on. I offer my apologies for any
confusion I may have caused; that was not my intent.
Anyway, here's a more serious response to your question, Farzan:
The answers to both your questions will almost certainly be found in any
C++ textbook, and could also be discovered through a few minutes of
googling. It's fine if you ask about a specific problem that's been giving
you trouble, but you should at least attempt to discover a solution before
asking us for the answers.
I originally wrote:
> 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.
|