-
Dec 19th, 2013, 12:43 PM
#1
Thread Starter
PowerPoster
overloading char* and string operators
heres my variant class and the test:
Code:
#include <iostream>
#include <string>
using namespace std;
class Variant
{
private:
string result;
public:
Variant()
{
}
template <typename T>
Variant(T val)
{
result=to_string(val);
}
Variant(string val)
{
result=val;
}
Variant(const char* val)
{
result=val;
}
Variant(const char val)
{
result=val;
}
Variant(bool val)
{
result=to_string(val);
if (result=="0")
{
result="false";
}
else
{
result="true";
}
}
friend ostream& operator<< (ostream &out, Variant &VResult)
{
out <<VResult.result;
return out;
}
friend istream& operator >> (istream &in, Variant &VResult)
{
getline(in,VResult.result);
return in;
}
operator double()
{
return atof(result.c_str());
}
operator char*()
{
return (char *) result.c_str();
}
};
int main()
{
string c;
Variant b="fdsgfds";
c=(string)b;
cout << c;
return 0;
}
using strings, i must casting them, but i want avoid that. but if i overload the string operator, enters in conflit with char*
can anyone advice me, please?
-
Dec 24th, 2013, 05:40 PM
#2
Re: overloading char* and string operators
Code:
Variant(bool val)
{
result=std::to_string(val);
if (result=="0")
{
result="false";
}
else
{
result="true";
}
}
Why why why?!
Code:
val ? std::string("true") : std::string("false");
What are your goals here for such an implementation (the full thing)? Also, put the private: interface at the bottom, below the public one. As a sidenote, to_string is C++11 compliant only, so if that raises a concern, it should be thought out.
Last edited by AceInfinity; Dec 24th, 2013 at 05:49 PM.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Dec 24th, 2013, 05:49 PM
#3
Thread Starter
PowerPoster
Re: overloading char* and string operators
Originally Posted by AceInfinity
Code:
Variant(bool val)
{
result=std::to_string(val);
if (result=="0")
{
result="false";
}
else
{
result="true";
}
}
Why why why?!
Code:
val ? std::string("true") : std::string("false");
What are your goals here for such an implementation (the full thing)?
1st - good chrismas
2nd - thanks for answer me
heres my new code:
Code:
// *** ADDED BY HEADER FIXUP ***
#include <cstdlib>
#include <iostream>
#include <string>
// *** END ***
#ifndef VARIANT_H_INCLUDED
#define VARIANT_H_INCLUDED
class variant
{
private:
string a="";
public:
variant()
{
a="";
}
variant (string value)
{
a=value;
}
variant (const char* value)
{
a=value;
}
variant (char* value)
{
a=value;
}
variant ( char value)
{
a=value;
}
template <typename T>
variant (T value)
{
a=to_string(value);
}
friend istream& operator >>(istream &is,variant &obj)
{
is>>obj.a;
return is;
}
friend ostream& operator <<(ostream &os,const variant &obj)
{
os<<obj.a;
return os;
}
friend istream &getline(istream &in, variant &s1)
{
getline(in, s1.a);
return in;
}
variant & operator = (int const & b)
{
a=to_string(b);
return *this;
}
variant & operator = (string const & b)
{
a=b;
return *this;
}
variant & operator = (double const & b)
{
a=to_string(b);
return *this;
}
variant & operator = (float const & b)
{
a=to_string(b);
return *this;
}
bool operator == (string const & b)
{
return (a==b);
}
operator string() const
{
return a; // return string member
}
operator char()
{
return a[0];
}
template<typename T>
operator T() const
{
return atof(a.c_str()) ;
}
};
#endif // VARIANT_H_INCLUDED
don't accept the char* for output, but i can use string and char
(sorry, but irealy want avoid the boost... so very dependencies(very headers files for variant\any)... i hate that... sorry, but i hate in that way and how is used)
these class is for accept any type and any type accept it
thanks for all and i realy accept more sugestions for these new class
-
Dec 24th, 2013, 06:06 PM
#4
Re: overloading char* and string operators
You're missing the point of include guards:
Code:
// *** ADDED BY HEADER FIXUP ***
#include <cstdlib>
#include <iostream>
#include <string>
// *** END ***
#ifndef VARIANT_H_INCLUDED
#define VARIANT_H_INCLUDED
->>
Code:
#ifndef VARIANT_H_INCLUDED
#define VARIANT_H_INCLUDED
#include <cstdlib>
#include <iostream>
#include <string>
The implementation here should be in a separate cpp source file too.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Dec 24th, 2013, 07:04 PM
#5
Re: overloading char* and string operators
Consider the following template:
MyVarType.h
Code:
#ifndef __MyVarType_H__
#define __MyVarType_H__
#include <iostream>
#include <string>
#include <fstream>
class MyVarType
{
public:
// Constructor Overloads
MyVarType();
#include "MyVarType.hxx"
MyVarType(std::string val);
MyVarType(const char* val);
MyVarType(const char val);
MyVarType(bool val);
// etc...
private:
std::string result;
};
#endif
MyVarType.hxx
Code:
template <typename T>
MyVarType(T val)
{
result = std::to_string(val);
}
MyVarType.cpp
Code:
#include "MyVarType.h"
// implementation here...
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Dec 25th, 2013, 07:57 AM
#6
Thread Starter
PowerPoster
Re: overloading char* and string operators
Originally Posted by AceInfinity
Consider the following template:
MyVarType.h
Code:
#ifndef __MyVarType_H__
#define __MyVarType_H__
#include <iostream>
#include <string>
#include <fstream>
class MyVarType
{
public:
// Constructor Overloads
MyVarType();
#include "MyVarType.hxx"
MyVarType(std::string val);
MyVarType(const char* val);
MyVarType(const char val);
MyVarType(bool val);
// etc...
private:
std::string result;
};
#endif
MyVarType.hxx
Code:
template <typename T>
MyVarType(T val)
{
result = std::to_string(val);
}
MyVarType.cpp
Code:
#include "MyVarType.h"
// implementation here...
sorry, can i put
'#include "MyVarType.hxx"'
inside of the class? and is portable?
(i always use it in file bigining)
-
Dec 25th, 2013, 03:21 PM
#7
Re: overloading char* and string operators
Originally Posted by joaquim
sorry, can i put
'#include "MyVarType.hxx"'
inside of the class? and is portable?
(i always use it in file bigining)
You need to read about how preprocessor directives like include work first to see why I've done it this way. All it does is allow the compiler to essentially replace that spot with the #include'd code declarations. You can put it inside the class depending on what the intention is. Portable? Why not? It's only an #include.
Although I should've added this above that include for the *.hxx:
Code:
template <typename T> MyVarType(T val);
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Dec 25th, 2013, 04:57 PM
#8
Thread Starter
PowerPoster
Re: overloading char* and string operators
Originally Posted by AceInfinity
You need to read about how preprocessor directives like include work first to see why I've done it this way. All it does is allow the compiler to essentially replace that spot with the #include'd code declarations. You can put it inside the class depending on what the intention is. Portable? Why not? It's only an #include.
Although I should've added this above that include for the *.hxx:
Code:
template <typename T> MyVarType(T val);
thanks for all... but what you tell me about class?? it needs more work in functions or it's fine?
it's better use the c++11 overloading operators or it's fine?
ok.. i must add the math operators, but it's cool
-
Dec 25th, 2013, 05:55 PM
#9
Re: overloading char* and string operators
Originally Posted by joaquim
thanks for all... but what you tell me about class?? it needs more work in functions or it's fine?
it's better use the c++11 overloading operators or it's fine?
ok.. i must add the math operators, but it's cool
C++11 just means that you'll need to be able to compile with C++11 standards, if this is supposed to be distributed to others for use, and those people may not like to use C++11 yet, or they just don't have things set up to compile C++11 compliant code, whatever the case may be. To me, C++11 is fairly well supported now, but there's still the odd case that people don't like to use it at this point in time.
And yes, you'll have to overload the operators all yourself for each type available. Especially since you can't really treat effectively, floating point datatypes, in the same way that you'd treat integer types, or you'll find a lot of hassle as a result, because there is nothing simple about the floating point datatypes.
As for your class, I don't think you quite understand yet how much more work will be required here, but as you go through supporting various datatypes that will be revealed.
Just do a search for floating point math and see. Here's a little bit of information I wrote about it a while back, just so that I could help people get a bit more insight about just how complex it is. 70% of the programmers out there I'll bet you just think that floating point datatypes are just simple datatypes with a decimal.
A quick introduction I wrote: http://tech.reboot.pro/showthread.php?tid=4058
A great link for key concepts: http://docs.oracle.com/cd/E19957-01/..._goldberg.html
Another thing I'll point out, is that the std::string, is a typedef for the basic_string<char> class, which manages the null terminator for you. If you do conversions between char*/char[] and std::string, make sure that you know when to add in '\0'.
Last edited by AceInfinity; Dec 25th, 2013 at 06:02 PM.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Dec 25th, 2013, 06:04 PM
#10
Thread Starter
PowerPoster
Re: overloading char* and string operators
Originally Posted by AceInfinity
C++11 just means that you'll need to be able to compile with C++11 standards, if this is supposed to be distributed to others for use, and those people may not like to use C++11 yet, or they just don't have things set up to compile C++11 compliant code, whatever the case may be. To me, C++11 is fairly well supported now, but there's still the odd case that people don't like to use it at this point in time.
And yes, you'll have to overload the operators all yourself for each type available. Especially since you can't really treat effectively, floating point datatypes, in the same way that you'd treat integer types, or you'll find a lot of hassle as a result, because there is nothing simple about the floating point datatypes.
As for your class, I don't think you quite understand yet how much more work will be required here, but as you go through supporting various datatypes that will be revealed.
i just used templates for numbers and the others for strings\char
but yes needs more work.. but seems cool. i use the c++11 because have very new things and, in my situation, i will share the mingw comipiler with my new language
but, of course, i can share my own class's: variant, console(the window size, caption, read, wirite and much more), math(with random(int min, int max)), multithread, sound, sprite, and more... inclued my own class's controls that i'm trying do it
-
Dec 25th, 2013, 06:07 PM
#11
Re: overloading char* and string operators
Originally Posted by joaquim
i just used templates for numbers and the others for strings\char
but yes needs more work.. but seems cool. i use the c++11 because have very new things and, in my situation, i will share the mingw comipiler with my new language
but, of course, i can share my own class's: variant, console(the window size, caption, read, wirite and much more), math(with random(int min, int max)), multithread, sound, sprite, and more... inclued my own class's controls that i'm trying do it
Huh?
You'd better make sure that this works for the cases I've hinted at above. What do you mean by "share the mingw compiler with my new language"? I thought the intent was to create your any-type, not your own language?
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Dec 25th, 2013, 06:13 PM
#12
Thread Starter
PowerPoster
Re: overloading char* and string operators
-
Dec 25th, 2013, 07:05 PM
#13
Re: overloading char* and string operators
but the variant only accept strings and numbers and not user defined data types
There's a good reason this should never be attempted in the first place however. That is over the top, and would only make the efficiency of the language worse. If the user defined type is meant to be interpreted as an int or a std::string, then LET IT, define itself as that through the class. There's no way you're going to be able to find out whether a conversion for a user defined type is even capable or worth converting to a variant type anyways, because you'll never know whether it's even worth converting to say an integer, or maybe even a container type.
1. You don't know what it encapsulates
2. You don't know what it should use for the conversion
3. You don't know if there is anything in that class that could be worthwhile converting
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Dec 26th, 2013, 06:16 AM
#14
Thread Starter
PowerPoster
Re: overloading char* and string operators
-
Dec 26th, 2013, 04:18 PM
#15
Re: overloading char* and string operators
imagine for a moment you need a int value?? then you need a boolean value?? and so on
These are still not user defined types... Imagine trying to convert a collection wrapper to a boolean? How would you do that? Or better yet, why? You don't know what this user defined type was meant to do, so how can you assume that it can or should be cast to any other type successfully? This is what I was trying to get at -- Think about it.
Global member variables aren't needed really, if you know what you're doing, because of how OOP and inheritance, as well as constructors and method parameters work. Why would you need a very large scope for variables that you'll never need 100% of the time? That's why I avoid them unless they are absolutely needed, but 90% of the time, I can find a reason not to have something global.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|