Advertisement

friend const astring & operator = (char * cstr, const astring & str);

Started by August 04, 2002 04:41 AM
15 comments, last by Senses777 22 years, 3 months ago
hey I was board, why not add another string library to the world, one with crazy functions like the decrement operator to cut off the last charactor (the greatest part is the prefix decrement operator cuts off the first charactor!), and the shift left and right operators to rotate the string. *slaps code clarity in the face* (by the way, anyone who can think of some other great obfuscating operators to add to my string class?) anyways, its been awhile since I''v posted here, but I have a legitimate question that has been answered a thousand times, so I am back! I''v never needed friend functions before, ever, throughout any of my projects. Thats the excuse for my ignorance... why cant I put in my class: friend const astring & operator = (char * cstr, const astring & str); Why does the = operator HAVE to be a member of a class? I''m not defining an operator for two basic types here! Thanks! .sen
"I want to make a simple MMORPG first" - Fenryl
I will take a stab at guessing.

Because the =operator is used to assign objects. This means there is a known object type which you are trying to assign to.

Unlike operators like == where it can be a friend function, it is because in that operation, the object doesn''t have to be a l-value. You can do these like 3 == Object, but you can''t do 3 = Object.

That''s why =operators must be member functions.
Advertisement
that seems dumb to me though, what if I wanted to make it an lvalue? why doesn''t C++ support this?

There is no operator of type = that has parameters (char * , astring) so why cant I override this? Seems like silliness to me.

If I wanted, I could make an == operator with params (char *, astring) and then I could make the first string equal to my astring inside! But then of coarse I cant have an == operator of that type, which is just annoying, not to mention complete code obfuscation. .sen
"I want to make a simple MMORPG first" - Fenryl
You can''t redefine how the operators work for built-in
types, period.
They are intrinsic to the language/compiler and cannot be changed.

Imagine the chaos that ensue if you can redefine "3 + 4" to
mean "3 - 4". Nobody wants that. Thus, the requirement
that one of the types on either side is a user-defined type.

In your example, the built-in type is char * . You
can''t change how equals(=) works for char * .

You have two options: provide a conversion operator for astring
or provide a member function to return a char * . The
latter is recommended.


Kami no Itte ga ore ni zettai naru!
神はサイコロを振らない!
tangents, you dont seem to understand my point. There is no operator=(char *, astring) function, anywhere. It is not intrinsic because the assignment operator is not automatically created for intrinsic types to user defined types, so why not allow me to do it?

You seem to think I want to redifine an existing operator such as operator = (char, char). I understand of coarse why you cannot overload this, it would be redefinition, not overloading.

If I am allowed to create a function in my class:

friend const astring & operator == (char * cstr, astring & str);
or
friend const astring & operator < (char * cstr, astring & str);

then why on earth cant I define the UNDEFINED operator:
friend const astring & operator = (char * cstr, astring & str);

this seems pointless to not allow the programmer to decide what he does and doesn''t want to overload on certain operators (NOTE: UNDEFINED OPERATORS!!!) .sen
"I want to make a simple MMORPG first" - Fenryl
Obfuscation tip: Overload the comma operator. Example:


  #include <iostream>using namespace std;#define print oh_teh_nos,struct oh_teh_nos_t { } oh_teh_nos;template <class T>oh_teh_nos_t &operator,(oh_teh_nos_t &lhs, T rhs) { cout << rhs; return lhs; }int main() {  print "Give me ", 10, " medals, please.\n"; // mwa ha ha  return 0;}  
Advertisement
thats just evil. But maybe I need operators that apply to my string class ^_^. Anyways, back to the subject, why am I not allowed to do what I said? .sen
"I want to make a simple MMORPG first" - Fenryl
I''m really not sure why such a restriction is in place.

As an evil alternative, perhaps you could define an implicit conversion to char*. It looks like this:

astring::operator char*() { return this->data; }

Now you can do things like:

astring foo("something something\n");
printf(foo);
char *bar = foo; // beware: bar now points to foo''s memory. Use strdup if you want a copy.
printf(bar);
What is it you trying to achieve? Make char* become astring or make astring behave like a char*? If it''s the former, you can''t change the behaviour of other class/types, that''s why the =op must be member function.

If latter, do what tangentz suggest. But I wouldn''t recommending making an implicit conversion to char* for a string class at all. See the std::string and read comp.lang.c++.moderated why the standard string doesn''t provide a conversion to char*.
quote: Original post by Beer Hunter
Obfuscation tip: Overload the comma operator. Example:


    #include <iostream>using namespace std;#define print oh_teh_nos,struct oh_teh_nos_t { } oh_teh_nos;template <class T>oh_teh_nos_t &operator,(oh_teh_nos_t &lhs, T rhs) { cout << rhs; return lhs; }int main() {  print "Give me ", 10, " medals, please.\n"; // mwa ha ha  return 0;}    



  --------------------Configuration: Junk - Win32 Debug--------------------Compiling...Junk.cppc:\program files\microsoft visual studio\vc98\include\xstring(44) : error C2678: binary '','' : no operator defined which takes a left-hand operand of type ''void'' (or there is no acceptable conversion)        c:\program files\microsoft visual studio\vc98\include\xstring(42) : while compiling class-template member function ''__thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >(const class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &)''Error executing cl.exe.Junk.exe - 1 error(s), 0 warning(s)  

This topic is closed to new replies.

Advertisement