![]() |
|
#21
|
||||
|
||||
|
Quote:
__________________
During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence? |
||||
|
#22
|
||||
|
||||
|
Now that the local brain-trust has allocated resources and applied inverse kinetics to this proposed problem I for one am curious how the COMMITTEE for SOLUTIONS has ruled. I have a feeling that there are more than one 'correct' solutions and all authors will be appropriately awarded their accolades.
Just my long winded way of saying, "Please show us a solution." It is a curious problem for those of us that are new. In fact, a seemingly impossible task. After all, how would it even be possible to change the output from outside the main() involving only an output director and a string literal? The only thing that I could come up with was possibly creating a version of cout or the << that sent the output desired instead of allowing the iostream version to work. The only way I can describe what I mean (at least in terms that I understand) is something similar (but more likely more complex) to this simple example: CPP / C++ / C Code:
Not that I have a clue how to solve the original problem, I am just speculating on possible solution theories. In my simple example I can control (at least I think so) my exit value by passing either one int or two and having the compiler send me to the function with the proper signature. If it is possible to overload (or overwelm as I sometimes think of it Inconceivable, I am more likely to avenge my father's death by finding a six fingered man. :-) __________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work." --Thomas Alva Edison "Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety." --Benjamin Franklin "A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes." --Hugh Downs |
|
#23
|
||||
|
||||
|
Well, I was waiting for a few more responses and then, to my chagrin, I deleted Dave's responses!
Dave wins by default since he is the only one that responded. Dave if you have your answers, I would appreciate your posting them... Funny enough, Dave's C answer is closer to my C++ answer and vice-a-versa. My C++: CPP / C++ / C Code:
What makes this a pain in the a**, is that once you overload << with a string literal, you can't use it in your overload function or it keeps calling itself! My C solution is much simpler: CPP / C++ / C Code:
The C solution kind of cheats (IMO) because it uses a pre-compiler directive, so the main code is "changed" before it is compiled. __________________
The best damn Sports Blog period. |
|
#24
|
|||||
|
|||||
|
Quote:
Hooray for Dave. 8-) Quote:
Ok, thanks D. I was on the right track. You 'stole' the call that would normally end up here, Quote:
Quote:
I know this doesn't follow the original question but adding the line to the original code would have bad results as well because of the additional <<. CPP / C++ / C Code:
Would output: Welcome to My World of Computing <-from << after cout Welcome to <-from << before endl <-from endl If I am correct is there a useful application to this other than forcing people like me to delve into the depths of header files. Where by the way there are gems like this to find. Quote:
Hmm.. compiling the example with the additional << and endl changes nothing. But, adding the line, CPP / C++ / C Code:
Welcome to My World of Computing Welcome to What the ... So it cout that is being overloaded. What would this be good for? Appending something like "<computer says>" then the actual output. Interesting stuff. Thanks to all. Thanks D, my brain is now a little more fullerer. :-) __________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work." --Thomas Alva Edison "Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety." --Benjamin Franklin "A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes." --Hugh Downs |
|
#25
|
||||
|
||||
|
My C solution is nearly identical to D's:
CPP / C++ / C Code:
__________________
During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence? |
|
#26
|
||||
|
||||
|
Thanks, guys, I was really curious to see what you will come up with for the C part!
I never thought about the precompiler directive, I have just "overriden" the printf; I'm not sure that it works on every compiler, so please be merciful about my code CPP / C++ / C Code:
I originally used puts for printing the text; this added a newline, so I STOLE the fputs from Walt As for the C++ thing, I didn't give it much thought since I read something similar in Stroustrup's "The C++ Programming Language" - "Operator Overloading"; there is also a very interesting test here; so, it's not like I could take any credit for that; d actually came up with a very good solution, with a great adding from Walt Can't wait to see Dave's code! Best regards, Luci __________________
Please read these Guidelines before posting on the forum "A person who never made a mistake never tried anything new." Einstein |
|
#27
|
||||
|
||||
|
Nope, it doesn't work on .NET
Guess the #define is the solution! __________________
Please read these Guidelines before posting on the forum "A person who never made a mistake never tried anything new." Einstein |
|
#28
|
||||
|
||||
|
Quote:
My opinion? This doesn't really serve much in and of itself, except to have a simple, straight forward example of how operator overloading is done. The real power of operator overloading is when you can overload the operator based upon a class that you have defined. Crystalattice has posted this sample here that shows the real use of operator overloading. The C sample? It can be very convenient to use #define such as Walt and I did. However, it does not seem to be very smart in this case as it replaces the printf function in all cases for this program. Again, a good simple sample showing how it is done, but useful, as it is implemented, for not much ![]() __________________
The best damn Sports Blog period. |
|
#29
|
|||
|
|||
|
Quote:
First of all, this is not an exercise in Good Things to do in your program. It's a puzzle. Even though I would never do any of these things in a "real" program, I feel that it is somewhat instructive to see what you can do within the rules of standard C and C++. In particular, I think it shows the dangers of macros and operator overloading. Both are very powerful tools but can lead to obfuscated code that is very difficult to debug. Well, I have two examples in C and one in C++. The first one in C is similar to the ones posted, except I used fprintf() instead of fputs(). CPP / C++ / C Code:
Here's another in C, that uses a local version of printf(). Note that in C the only special function name is main(). You had better not have anything in your namespace called main. Everything else is fair game (except, of course reserved keywords like if, for, etc.) [edit on November 8, 2004] I probably should have used fputs(st, stdout) instead of fputs(st) in the following, since puts() appends a newline, and, therefore changes the output. (Now, I say that my program as shown here does print out "Welcome to My World of Computing", as required in the rules, and I always follow the rules. However, some have said that it was implied that the original program's output should be unchanged except for prepending "Welcome to ". Oh, well...) Dave [/edit] CPP / C++ / C Code:
Finally, the C++ routine, also using a macro. (Maybe this is why Bjarne Stroustroup has said that he would like to eliminate macros from the face of the earth, or something like that.) CPP / C++ / C Code:
Regards, Dave Last edited by davekw7x : 08-Nov-2004 at 10:54.
|
|
#30
|
||||
|
||||
|
Thanks for the replies everyone. I just wanted to make sure. (never hurts to ask especially here) It is very interesting and I do have a much better understanding of the raw power of overloading (for good and evil). Just because you can do something doesn't mean you should.
Well, on to new things for me, thanks for the lesson. __________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work." --Thomas Alva Edison "Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety." --Benjamin Franklin "A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes." --Hugh Downs |
Recent GIDBlog
Problems with the Navy (Chiefs) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| help with coding expectation maximization needed. thanks | edge | C Programming Language | 8 | 08-Nov-2005 06:00 |
| Some small things | Allowee | GIDSearch™ | 6 | 14-Jul-2004 02:40 |
| This is a small snippet from a much larger piece. | Tang_Quester | Open Discussion Forum | 1 | 19-Mar-2004 02:17 |
| Free 1st month / Free setup / No credit card needed...Plans start at 4.95 | LarryIsaac | Web Hosting Advertisements & Offers | 0 | 11-Oct-2003 15:03 |
| Script needed for letting user input a few days of data for tracking and analysis. | tradertt | MySQL / PHP Forum | 3 | 06-Mar-2003 03:54 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The