ENIGMA Forums
Sharing is caring => Tips, Tutorials, Examples => Topic started by: RetroX on February 24, 2010, 01:47:59 pm
-
Something useful for ENIGMA:
std::ostream &operator <<(std::ostream &outstream,__variant &variable)
{
if (variable.type)
{
return outstream << variable.doubleval;
}
return outstream << variable.stringval;
}
std::istream &operator >>(std::istream &instream,__variant &variable)
{
char c,c2;
c=instream.get();
if (c>=48 && c<=57)
{
instream.putback(c);
return instream >> variable.doubleval;
}
else if (c==46)
{
c2=instream.get();
if (c2>=48 && c2<=57)
{
instream.putback(c2);
instream.putback(c);
return instream >> variable.doubleval;
}
}
instream.putback(c);
return instream >> variable.stringval;
}
You probably should add a typecast operator for var to cast to __variant.
This is from r3's source, though, so, IDK what you've changed.
Also, because I wouldn't know, would this also work for iostream, fstream, and stringstream because they are inherited from istream and ostream, or would it not because this is defined after that?
-
Does... that work? It should error that your function should be a member of std::ostream rather than declared in the global scope...
-
No, you can declare new versions of << without it being a member. That was the whole point of switching to the brain-dead system.
-
Duly noted.
-
Duly noted.
Yeah, I figured that out recently, and decided to mess around with it.
typedef char int8;
typedef unsigned char uint8;
std::ostream &operator <<(std::ostream &outstream,int8 value)
{
return outstream << (int)value;
}
std::ostream &operator <<(std::ostream &outstream,uint8 value)
{
return outstream << (unsigned int)value;
}
-
Additionally, here's something else that might be useful:
// Operators for strings (addition)
string &operator +(string &str1,string &str2)
{
string temp=str1;
return temp+=str2;
}
string &operator +(string &str1,cstring str2)
{
string temp=str1;
return temp+=str2;
}
// Operators for strings (comparison)
bool operator ==(string &str1,string &str2)
{
string temp=str1;
return (temp.compare(str2));
}
bool operator ==(string &str1,cstring str2)
{
string temp=str1;
return (temp.compare(str2));
}
// Operators for strings (comparison)
bool operator !=(string &str1,string &str2)
{
return !(str1==str2);
}
bool operator !=(string &str1,cstring str2)
{
return !(str1==str2);
}
Simple operators for strings.
-
String already defines those...
And then some...
-
I hate it when cplusplus.com doesn't provide an accurate reference.
I learned something new, today, I guess.
-
Those functions are on cplusplus.com:
http://cplusplus.com/reference/string/operator+/ (http://cplusplus.com/reference/string/operator+/)
http://cplusplus.com/reference/string/operators/ (http://cplusplus.com/reference/string/operators/)
-
Those functions are on cplusplus.com:
http://cplusplus.com/reference/string/operator+/ (http://cplusplus.com/reference/string/operator+/)
http://cplusplus.com/reference/string/operators/ (http://cplusplus.com/reference/string/operators/)
I was looking at <cstring>; for whatever reason, it didn't occur to me to look at <string>.
-
Could you implement those operators for char *s?
-
Could you implement those operators for char *s?
string operator+(string str,char *str2)
{
return str+const_cast<const char*>(str2);
}
?
-
Not for two char*s though. C++ doesn't allow that.
-
Erm, why would you want to do that? That's kind of the point of string - to add things like that.
-
He's thinking about 'situations where you need to quote "' + "that's" + '," or other situations requiring both "' + " and '".
-
That's const char*
char* would mean something like this:
char x[]={'h','e','l','l','o',',',' ','\0'};
char y[]={'d','a','m','n','i','t','\0'};
char *z=x+y;
or
char *z=const_cast<char*>("hello, ")+const_cast<char*>("damnit");
-
The only difference between const char* and char* is that const char* points to read-only memory. And even that's only on systems that support such (ie, not Windows). '\0' == 0, and I'd be willing to bet that if you were to compare {'n','o',0} and "no" you'd get that they were equivalent. And I do mean as-is.
"Test" == "test" compares correctly for string literals. "Test" == "test" will return false, "test" == "test" will return true. This is because they point to the same location in memory when GCC is done with them.
Also, you don't need to use const_cast to get it represented as a const char. :P
Const char* can be set to a char* without cast. Vice-versa requires cast, but is dangerous on Linux and the like.
-
The only difference between const char* and char* is that const char* points to read-only memory. And even that's only on systems that support such (ie, not Windows). '\0' == 0, and I'd be willing to bet that if you were to compare {'n','o',0} and "no" you'd get that they were equivalent. And I do mean as-is.
"Test" == "test" compares correctly for string literals. "Test" == "test" will return false, "test" == "test" will return true. This is because they point to the same location in memory when GCC is done with them.
Also, you don't need to use const_cast to get it represented as a const char. :P
Const char* can be set to a char* without cast. Vice-versa requires cast, but is dangerous on Linux and the like.
Whenever I have tried to do it without const_cast, G++ bugs me about a depreciated conversion (that works). I always use -Wall and purge all warnings and errors, and that is one that I happen to get into the habit of doing.
-
That's const char*
char* would mean something like this:
char x[]={'h','e','l','l','o',',',' ','\0'};
char y[]={'d','a','m','n','i','t','\0'};
char *z=x+y;
or
char *z=const_cast<char*>("hello, ")+const_cast<char*>("damnit");
Wait, what?
-
That's const char*
char* would mean something like this:
char x[]={'h','e','l','l','o',',',' ','\0'};
char y[]={'d','a','m','n','i','t','\0'};
char *z=x+y;
or
char *z=const_cast<char*>("hello, ")+const_cast<char*>("damnit");
Wait, what?
Exactly. It's pointless.
-
That's const char*
char* would mean something like this:
char x[]={'h','e','l','l','o',',',' ','\0'};
char y[]={'d','a','m','n','i','t','\0'};
char *z=x+y;
or
char *z=const_cast<char*>("hello, ")+const_cast<char*>("damnit");
Wait, what?
Exactly. It's pointless.
Tell me...
char x[]={'h','e','l','l','o',',',' ','\0'};
char y[]={'d','a','m','n','i','t','\0'};
char *z=x+y;
printf("%c",z[2]);
...What happens when you run that? If you say "prints l" I'll shoot your face off. If you say "access violation because you added 2 random pointers", then I'll forgive you... for now.
-
Of course it's adding two random pointers. He was explaining what I meant when I asked if you could overload operator+ for two char *s.
-
I see, though overloading the + for a char* is really pointless IMO.
-
Yeah, compiler presently concatenates "" + "" automatically. For "" + anything else, I want it to cast first "" to variant.
-
It concatenates "" "" automatically. someCharPtr + "" is what might be useful to overload, to avoid std::string constructor noise. However, I agree that it's not very useful. Allowing operator overloading on arbitrary types would make the language completely unpredictable; it'd be worse than monkeypatching in Ruby.