Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Chapter 9 Strings Visual C++ and MFC Fundamentals


strcpy(Producer, Artist);

int Len = strlen(Producer);
pDC->TextOut(20, 20, Producer, Len);
}

To make a copy of a string variable, you can use the assignment operator. The primary
technique applied to this operator can be used to initialize a string variable declared using
the default constructor. Here is an example:

void CCView1View::OnDraw(CDC* pDC)
{
CCView1Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
string StrName = "Central African Republic";

int Len = StrName.length();
pDC->TextOut(20, 22, StrName.c_str(), Len);
}

Using the assignment operator, you can copy one string variable into another. To do this,
simply assign one variable to another. Here is an example:

void CExerciseView::OnDraw(CDC* pDC)
{
CExerciseDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);

string Artist = _T("Arsene Wendt");
string Producer;

Producer = Artist;

int Len = Producer.length();
pDC->TextOut(20, 20, Producer.c_str(), Len);
}

Besides the assignment operator, to copy a string variable or to assign the value of one
string to another, you can also use the string::assign() method to assign a string. It is
provides in various syntaxes as follows:

basic_string& assign(const E *s);
basic_string& assign(const E *s, size_type n);
basic_string& assign(const basic_string& str, size_type pos, size_type n);
basic_string& assign(const basic_string& str);
basic_string& assign(size_type n, E c);
basic_string& assign(const_iterator first, const_iterator last);

Here is an example:

void CExerciseView::OnDraw(CDC* pDC)
{
CExerciseDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);

string Artist = _T("Arsene Wendt");
Free download pdf