Convert Custom type 'String' to std::string

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2007
Posts: 42
Reputation: vs49688 is an unknown quantity at this point 
Solved Threads: 1
vs49688 vs49688 is offline Offline
Light Poster

Convert Custom type 'String' to std::string

 
0
  #1
Jun 1st, 2009
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:
  1. SetVar::operator char* ()
  2. {
  3. if (szName == "Passwd")//only create a md5 hash for the password, not anything else
  4. {
  5. std::string pwd;
  6. //here i need to convert 'sVal' from a String to a std::string called 'pwd'
  7. return md5.getHashFromString(pwd);
  8. }
  9. return sVal;
  10. }

types.cpp
  1. // types.cpp
  2. //
  3.  
  4. #include "types.h"
  5.  
  6. //
  7. // String
  8. //
  9.  
  10. String::String()
  11. {
  12. szStr = NULL;
  13. }
  14.  
  15. String::String(char *str)
  16. {
  17. *this = str;
  18. }
  19.  
  20. String::~String()
  21. {
  22. if (szStr) {
  23. free(szStr);
  24. szStr = NULL;
  25. }
  26. }
  27.  
  28. String& String::operator= (char *str)
  29. {
  30. if (szStr) {
  31. free(szStr);
  32. szStr = NULL;
  33. }
  34.  
  35. if (str)
  36. szStr = strdup(str);
  37.  
  38. return *this;
  39. }
  40.  
  41. String::operator char* ()
  42. {
  43. return szStr;
  44. }
  45.  
  46. bool String::operator== (char *str)
  47. {
  48. if (!strcmp(szStr, str))
  49. return true;
  50. else
  51. return false;
  52. }

types.h
  1. // types.h
  2. //
  3.  
  4. #ifndef TTYPES_H
  5. #define TTYPES_H
  6.  
  7. #include <windows.h>
  8.  
  9. typedef unsigned char uchar;
  10. typedef unsigned short ushort;
  11. typedef unsigned int uint;
  12. typedef unsigned long ulong;
  13.  
  14. #define WSTD_DEL(x) { if ((x)) { delete (x); (x)=NULL; } }
  15. #define WSTD_DELM(x) { if ((x)) { delete[] (x); (x)=NULL; } }
  16.  
  17. //
  18. // String
  19. //
  20.  
  21. class String
  22. {
  23. public:
  24. String();
  25. String(char *str);
  26. ~String();
  27.  
  28. String& operator= (char *str);
  29. operator char* ();
  30.  
  31. bool operator== (char *str);
  32.  
  33. private:
  34. char *szStr;
  35. };
  36.  
  37. #endif

Thanks in advance.
vs49688
Last edited by vs49688; Jun 1st, 2009 at 11:18 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 320
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 63
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: Convert LPCTSTR to std::string

 
0
  #2
Jun 1st, 2009
Let start with..
  1. //...
  2. md5.getHashFromString(pwd.c_str());
  3. //...
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 32
Reputation: Ahmed_I is an unknown quantity at this point 
Solved Threads: 1
Ahmed_I Ahmed_I is offline Offline
Light Poster

Re: Convert LPCTSTR to std::string

 
0
  #3
Jun 1st, 2009
Try using this routine to convert from cstring to std::string using stringstream
Here is the routine
  1. #include <sstream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. string convert(char *customString)
  7. {
  8. stringstream s;
  9. s << customString;
  10. string d = s.str();
  11. return d;
  12.  
  13. }

Hope that i understood your Question.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,340
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1458
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Convert Custom type 'String' to std::string

 
0
  #4
Jun 1st, 2009
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;
};
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 42
Reputation: vs49688 is an unknown quantity at this point 
Solved Threads: 1
vs49688 vs49688 is offline Offline
Light Poster

Re: Convert Custom type 'String' to std::string

 
0
  #5
Jun 1st, 2009
thanks guys. Ancient Dragon's solution solved it.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Convert Custom type 'String' to std::string

 
0
  #6
Jun 1st, 2009
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?
  1. String::operator std::string() const
  2. {
  3. return szStr;
  4. }
3. Avoid using of malloc/strdup/free stuff in C++ programs. Use new/delete operators only.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC