converting to const char*

S

sadegh

I wrote a function to convert any type to string:

template <typename T>
std::string toStr(const T &thing) {
std::eek:stringstream outStr;
outStr << thing;
return outStr.str();
}

the above function works properly. I also wrote another function to
convert any type to const char* but it doesent work properly

template <typename T>
const char* toCStr(const T &thing) {
std::eek:stringstream outStr;
outStr << thing;
std::string Str=outStr.str();
return Str.c_str();
}

Does any body know what is its problem?

Thanks
 
A

Alf P. Steinbach

* sadegh:
template <typename T>
const char* toCStr(const T &thing) {
std::eek:stringstream outStr;
outStr << thing;
std::string Str=outStr.str();
return Str.c_str();
}

Does any body know what is its problem?

You're returning a pointer to a an object that no longer exists after
returning; this is known as a "dangling" pointer (or reference).
 
G

Gianni Mariani

sadegh said:
So what shoud I do to convert any type to const char*?

Does the caller expect to have to delete the memory on the returned string ?
 
S

sadegh

Does the caller expect to have to delete the memory on the returned string ?

I dont know. but for example I want to call the toCStr() function like
these:

AfxMessageBox(toCStr(8)); // showing the number "8"

or

m_EditBox.SetWindowText(toCStr(8)); // insert the number "8" into an
edit box

Tanks.
 
A

aiooua

template <typename T>
const char* toCStr(const T &thing) {
std::eek:stringstream outStr;
outStr << thing;
std::string Str=outStr.str();
return Str.c_str();

}
Does any body know what is its problem?

you need to ensure the converted string lives outside the scope of
this function. one simple way is to do that is:
--
template <typename T>
const char* toCStr(const T &thing) {
static std::eek:stringstream outStr;
outStr.str("");
outStr << thing;
return outStr.str().c_str();
}
--
note that there's only one covnersion buffer, all users of toCStr
would need to make deep copies of the result and not rely on its
contents.

thanks,
 
G

Gianni Mariani

sadegh said:
I dont know. but for example I want to call the toCStr() function like
these:

AfxMessageBox(toCStr(8)); // showing the number "8"

or

m_EditBox.SetWindowText(toCStr(8)); // insert the number "8" into an
edit box

What you want is an object that extends the life of the string until the
function call returns. The catch with this is that the lifetime of the
string is very short, although longer than a dead return value. Once
the expression is evaluated, the memory of the string is no longer
valid. Hence, don't be caught holding on to a pointer from the toCStr
below otherwise you will run into nasty probs.

// toCStr header

#include <sstream>
#include <string>

template <typename T>
std::string ToString( const T & val )
{
std::eek:stringstream l_out;
l_out << val;
return l_out.str();
}

struct toCStr
{
const std::string m_value;

template <typename T>
toCStr( const T & ival )
: m_value( ToString( ival ) )
{
}

operator const char * () const
{
return m_value.c_str();
}
};

/// example code

#include <iostream>

void foo( const char * str )
{
std::cout << str << "\n";
}

int main()
{
foo( toCStr( 8 ) );
}
 
S

sadegh

What you want is an object that extends the life of the string until the
function call returns. The catch with this is that the lifetime of the
string is very short, although longer than a dead return value. Once
the expression is evaluated, the memory of the string is no longer
valid. Hence, don't be caught holding on to a pointer from the toCStr
below otherwise you will run into nasty probs.

// toCStr header

#include <sstream>
#include <string>

template <typename T>
std::string ToString( const T & val )
{
std::eek:stringstream l_out;
l_out << val;
return l_out.str();

}

struct toCStr
{
const std::string m_value;

template <typename T>
toCStr( const T & ival )
: m_value( ToString( ival ) )
{
}

operator const char * () const
{
return m_value.c_str();
}

};

/// example code

#include <iostream>

void foo( const char * str )
{
std::cout << str << "\n";

}

int main()
{
foo( toCStr( 8 ) );



}- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -

What a nice answer and a nicer example. THANK YOU VERY MUCH Gianni.
 
J

johan13

I wrote a function to convert any type to string:

template <typename T>
std::string toStr(const T &thing) {
std::eek:stringstream outStr;
outStr << thing;
return outStr.str();

}

You're reinventing the wheel there. Have a look at
boost::lexical_cast. It is very similar to your code. http://www.boost.org/

- Johan Levin
 
R

Ron Natalie

aiooua said:
template <typename T>
const char* toCStr(const T &thing) {
static std::eek:stringstream outStr;
outStr.str("");
outStr << thing;
return outStr.str().c_str();
}
--
note that there's only one covnersion buffer, all users of toCStr
would need to make deep copies of the result and not rely on its
contents.

thanks,

Here's one place where a macro has a tiny bit of use.

template <typename T> std::string toStr(const T& thing) {
std::eek:stringstream outStr;
outStr << thing;
reutrn outStr.str();
}

#define toCStr(t) (toStr(t).c_str())

The temporary string returned by toStr() (and the c_str() from it)
will last until the end of thefull expression.
 
S

sadegh

Here's one place where a macro has a tiny bit of use.

template <typename T> std::string toStr(const T& thing) {
std::eek:stringstream outStr;
outStr << thing;
reutrn outStr.str();

}

#define toCStr(t) (toStr(t).c_str())

The temporary string returned by toStr() (and the c_str() from it)
will last until the end of thefull expression.

An Excellent Answer!!!!!!!!!!!! you are very smart Ron.
 
J

James Kanze

So what shoud I do to convert any type to const char*?

You shouldn't. Any more than you should try to generically
convert any type to string---it just doesn't make sense.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top