sandy@murdocks.on.ca wrote in message
<1164773518.311928.51100@14g2000cws.googlegroups.c om>...
Quote:
>Thanks Bob.
>
>It works, and I understand it... but I don't (how's that for confusing)
>
>I understand that it was calling the wrong toupper... but since the
>original toupper takes a char, and mine takes a string, I thought it
>was like regular overloading and it would be taken care of...
>Hmmm... Like I say, I now know what it was doing; I am just not sure
>what that means about overloading.
>
>I am a student and we have to overload pretty much everything in our
>assignments (=, ==, <, etc.) and I thought this would work the same
>way...
>Thanks.
>I will have to think about this a bit.
You could have overloaded 'toupper' in the class:
class ToUpTest{ public:
void Print(std::string str, std::ostream &sos){
sos<<toupper(str)<<std::endl;
return;
}
std::string toupper(std::string S){
// using std::toupper; // another way
for( size_t i(0); i < S.size(); ++i ){
S[ i ] = toupper(S[ i ]);
//S[ i ] = std::toupper(S[ i ]);
//S[ i ] = ::toupper(S[ i ]);
}
return S;
}
char toupper(char S){
return std::toupper( S );
}
// but, this is kind of silly since you can go direct.
};
I think you should bring up this problem in your programming class. Hopefully
the instructor will allow the other students to (try to) explain it. Lessons
learned that way seem to 'stick' better. <G>
This also works (re-name):
class ToUpTest{ public:
void Print(std::string str, std::ostream &sos){
sos<<ToUpper(str)<<std::endl;
return;
}
std::string ToUpper(std::string S){ // note rename
for( size_t i(0); i < S.size(); ++i){
S[ i ] = toupper(S[ i ]);
}
return S;
}
};
So why?
[ the following: I may be feeding you fact or fiction. I'm not trying to
confuse you. Just invoke some thoughts.]
// ------
Think about what happens when you overload (=, ==, <, etc.). The
compiler would provide (some) defaults if you didn't overload, right? But,
when you overload, you take responsibility. Could that be it?
struct A{
int func( int ){ return 42;}
};
struct B{
double func( double ){ return 42.9;}
};
int num(0);
B b;
int num2 = b.func( num );
Would you expect A::func(int) to be called? Think what would happen if it
did. Could there ever be encapsulation?
// ------ <end single-brain-cell tyrannical tirade>
Well, run it around in your head for a while, but, before it gets to the
migraine level, come back here and we'll wring it out.
[ hopefully with the aid of some of the *real* experts. ]
--
Bob R
POVrookie