943,831 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4971
  • C++ RSS
Jun 1st, 2009
0

Convert Custom type 'String' to std::string

Expand Post »
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:
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Light Poster
vs49688 is offline Offline
48 posts
since Dec 2007
Jun 1st, 2009
0

Re: Convert LPCTSTR to std::string

Let start with..
c++ Syntax (Toggle Plain Text)
  1. //...
  2. md5.getHashFromString(pwd.c_str());
  3. //...
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008
Jun 1st, 2009
0

Re: Convert LPCTSTR to std::string

Try using this routine to convert from cstring to std::string using stringstream
Here is the routine
CPP Syntax (Toggle Plain Text)
  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.
Reputation Points: 14
Solved Threads: 1
Light Poster
Ahmed_I is offline Offline
32 posts
since Nov 2008
Jun 1st, 2009
0

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

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;
};
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,951 posts
since Aug 2005
Jun 1st, 2009
0

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

thanks guys. Ancient Dragon's solution solved it.
Reputation Points: 10
Solved Threads: 1
Light Poster
vs49688 is offline Offline
48 posts
since Dec 2007
Jun 1st, 2009
0

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

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?
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Allegro 5 OS type
Next Thread in C++ Forum Timeline: uninitialized local variable





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC