Member Avatar for vs49688

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:

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

// 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

// 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

Recommended Answers

All 5 Replies

Let start with..

//...
md5.getHashFromString(pwd.c_str());
//...

Try using this routine to convert from cstring to std::string using stringstream
Here is the routine

#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;
};
Member Avatar for vs49688

thanks guys. Ancient Dragon's solution solved it.

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?

String::operator std::string() const
{
    return szStr;
}

3. Avoid using of malloc/strdup/free stuff in C++ programs. Use new/delete operators only.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.