S
S
Sergey Sova2014-08-20 03:30:05
Programming
Sergey Sova, 2014-08-20 03:30:05

How to concatenate strings and assign a value to a variable in a class?

I have a class

class Tampler
{
public:
Tampler();
private:
const char* m_sMyString;
};

And there is a set of lines
#define TLDR_EXT ".tldr"
const char* GetCurrentDir();
const char *filename = "filename";

I need to combine the received lines into one in the constructor and assign the value to m_sMyString;
How to do it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
EXL, 2014-08-20
@LestaD

Excuse me, what's the problem? Depending on the situation, you can use both the C++ approach:

Tampler()
{
        std::string str(GetCurrentDir());
        str = str + filename + TLDR_EXT;
        m_sMyString = strdup(str.c_str());
}
So is the C approach:
Tampler()
{
        char *tempstr = (char *)malloc(strlen(GetCurrentDir()) + strlen(filename) + strlen(TLDR_EXT) + 1);
        strcpy(tempstr, GetCurrentDir());
        strcat(tempstr, filename);
        strcat(tempstr, TLDR_EXT);
        m_sMyString = tempstr;
}

If, of course, I understood you correctly.

A
AxisPod, 2014-08-20
@AxisPod

Change const char* to std::string and don't sweat it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question