| | |
Convert Custom type 'String' to std::string
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2007
Posts: 42
Reputation:
Solved Threads: 1
EDIT: I mucked up the thread title. It should be "Convert Custom type 'String' to std::string"
Hi,
How would I be able to convert a custom type 'String' to a std::string? Just that the function I'm using requires a std::string.
troublesome code:
types.cpp
types.h
Thanks in advance.
vs49688
Hi,
How would I be able to convert a custom type 'String' to a std::string? Just that the function I'm using requires a std::string.
troublesome code:
C++ Syntax (Toggle Plain Text)
SetVar::operator char* () { if (szName == "Passwd")//only create a md5 hash for the password, not anything else { std::string pwd; //here i need to convert 'sVal' from a String to a std::string called 'pwd' return md5.getHashFromString(pwd); } return sVal; }
types.cpp
C++ Syntax (Toggle Plain Text)
// types.cpp // #include "types.h" // // String // String::String() { szStr = NULL; } String::String(char *str) { *this = str; } String::~String() { if (szStr) { free(szStr); szStr = NULL; } } String& String::operator= (char *str) { if (szStr) { free(szStr); szStr = NULL; } if (str) szStr = strdup(str); return *this; } String::operator char* () { return szStr; } bool String::operator== (char *str) { if (!strcmp(szStr, str)) return true; else return false; }
types.h
C++ Syntax (Toggle Plain Text)
// types.h // #ifndef TTYPES_H #define TTYPES_H #include <windows.h> typedef unsigned char uchar; typedef unsigned short ushort; typedef unsigned int uint; typedef unsigned long ulong; #define WSTD_DEL(x) { if ((x)) { delete (x); (x)=NULL; } } #define WSTD_DELM(x) { if ((x)) { delete[] (x); (x)=NULL; } } // // String // class String { public: String(); String(char *str); ~String(); String& operator= (char *str); operator char* (); bool operator== (char *str); private: char *szStr; }; #endif
Thanks in advance.
vs49688
Last edited by vs49688; Jun 1st, 2009 at 11:18 am.
•
•
Join Date: Nov 2008
Posts: 32
Reputation:
Solved Threads: 1
Try using this routine to convert from cstring to std::string using stringstream
Here is the routine
Hope that i understood your Question.
Here is the routine
CPP Syntax (Toggle Plain Text)
#include <sstream> #include <string> using namespace std; string convert(char *customString) { stringstream s; s << customString; string d = s.str(); return d; }
Hope that i understood your Question.
The same way std::string does it
class String
{
public:
String();
String(char *str);
~String();
String& operator= (char *str);
operator char* ();
const char* c_str() {return szStr;}
bool operator== (char *str);
private:
char *szStr;
}; 1. By the way, you must define non-trivial copy constructor and assignment operator for this class String (or declare them as private to prevent inevitable program crash if default copy constructor and/or assignment will be called).
2. If you want String to std::string conversion, define it, what's a problem?
3. Avoid using of malloc/strdup/free stuff in C++ programs. Use new/delete operators only.
2. If you want String to std::string conversion, define it, what's a problem?
C++ Syntax (Toggle Plain Text)
String::operator std::string() const { return szStr; }
![]() |
Similar Threads
- How do you convert a character type into a string type?? (Java)
- Convert object of type string to a string (C++)
- i can't seem to get this to work (C++)
- Search string? (C++)
- cast from type ‘DBNull’ to type ‘String’ is not valid (VB.NET)
- Cast from type 'DBNull" to type 'String' is not valid (ASP.NET)
- STL priority_queue with custom type...how do I do iit (C++)
Other Threads in the C++ Forum
- Previous Thread: Allegro 5 OS type
- Next Thread: uninitialized local variable
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings struct temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






