| | |
Convert Custom type 'String' to std::string
![]() |
•
•
Join Date: Dec 2007
Posts: 43
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 10: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
Views: 4583 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C++
algorithm api array arrays assignment basic beginner binary c++ c++borland c/c++ calculator char class classes code compile compiler constructor conversion convert count delete desktop dll dynamic encryption error file files form fstream function functions game givemetehcodez graph gui homework i/o iamthwee input int integer lazy library linker list loop looping math matrix member memory network newbie news number numbers object objects opengl output parameter pointer pointers problem program programming project qt random read recursion recursive reference return search sort sorting spoonfeeding string strings struct student studio template templates text time tree variable vc++ vector video visual win32 window windows winsock






