Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

class CWord:public CString process of assignment education.

Posted on 1999-01-07
6
Medium Priority
?
230 Views
Last Modified: 2013-11-20
class CWord : public CString  
{
public:
      CWord();
      virtual ~CWord();
//      CWord(const CString &stringSrc);

      CString ParseNChar(unsigned int iChars);
      CString SubWord(int iStartWord, int iNextNWords=0);
      void ChangeDelimiterList(CString sList);
      CString Word(int word);
      const CString& operator=(const CString &stringSrc);
private:
      CString m_sDelimiterList;
};
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CWord::CWord()
{
      m_sDelimiterList=" ";
}

CWord::~CWord()
{

}

//CWord::CWord(const CString & stringSrc)
//{
//      CString::CString(stringSrc);
//}

const CString& CWord::operator =(const CString & stringSrc)
{
      CString::operator=(stringSrc);
      return *this;
}

CString CWord::Word(int word)
{
      char *pChar=_strdup(GetBuffer(GetLength()));
      CString sRet;
      ReleaseBuffer();

      if(word>=1)
      {
            pChar=strtok(pChar, m_sDelimiterList);
            if(word==1)
            {
                  sRet=pChar;
                  free(pChar);
                  return sRet;
            }
            else
            {
                  for(int i=0;i<word-1;i++)
                  {
                        pChar=strtok(NULL, m_sDelimiterList);
                  }
                  return pChar;
            }
      }
      else
      {
            return "";
      }
}

void CWord::ChangeDelimiterList(CString sList)
{
      m_sDelimiterList=sList;
}

CString CWord::SubWord(int iStartWord, int iNextNWords)
{
      char*pChar=_strdup(GetBuffer(GetLength()));
      int iWord=0;
      CString sRet;
      ReleaseBuffer();

      if((iStartWord<1)||(pChar==NULL))
            return "";

      pChar=strtok(pChar, m_sDelimiterList);
      iWord++;
      while(iStartWord!=iWord)
      {
            pChar=strtok(NULL, m_sDelimiterList);
            iWord++;
      }
      sRet=pChar;
      iNextNWords--;
      while((iNextNWords!=0)&&(pChar=strtok(NULL, m_sDelimiterList)))
      {
//            pChar=strtok(NULL, m_sDelimiterList);
            if(pChar!=NULL)
            {
                  sRet+=" ";
                  sRet+=pChar;
            }
            iNextNWords--;
      }
      return sRet;
}

CString CWord::ParseNChar(unsigned int iChars)
{
      char *pChar=_strdup(GetBuffer(GetLength()));
      CString sRet;
      ReleaseBuffer();

      for(unsigned int i=0;i<iChars;i++)
      {
            sRet+=pChar[i];
      }
      _mbsnbset((unsigned char *)pChar, ' ', iChars);
      CString::operator=(pChar);
      TrimLeft();
      return sRet;
}
Everything compiles fine (should do what I want right!):) but the indicated line below fails to put the CString into the CWord.

      CFlexCheckDoc* pDoc = GetDocument();
      CString sTemp=pDoc->m_sFlexData;
      CString sTTemp;
-->      CWord wWord=sTemp;
When I make the change as follows the assignment works fine
      CFlexCheckDoc* pDoc = GetDocument();
      CString sTemp=pDoc->m_sFlexData;
      CString sTTemp;
      CWord wWord;
      wWord=sTemp;
I am looking for some one who can break it down for me why this is happening.  You know not repeat the same mistake.

Thanks Guys.
0
Comment
Question by:graber
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 1

Expert Comment

by:dd_b
ID: 1327365
Hi the problem is you have = operator overloaded, but you don't have constructor with CString as parameter.  ( commented in your case )

whenever u say

CWord word = "some string"  // this envokes  constructor with CString not
                                                          // = operator.with CString

& incase of

CWord word;// creates an object of CWord
word = "some string" // this will invoke your = operator

this is the only different.

to run this remove comment of constructor with CString

CWord::CWord( CString & str )

OK.
0
 
LVL 2

Author Comment

by:graber
ID: 1327366
dd b
Ok I am following you.  As the lines that I commented out suggest I did have it as part of the code.  When I used the assignment as you suggested it was never completed.  I did a trace and found that no sooner was the constructor called that the destructor was called and the string never made it into the new variable.  Can you expound on this please.

Thanks for you timely response.


0
 
LVL 2

Author Comment

by:graber
ID: 1327367
I need an explaination of why
//CWord::CWord(const CString & stringSrc)
//{
     // CString::CString(stringSrc);
//}
is not filling it's self with the string passed int
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Accepted Solution

by:
Tommy Hui earned 400 total points
ID: 1327368
Because all you are doing in CWord's constructor is call the base class's constructor in its definition. You are not initializing the base class. To initialize it, you will need

CWord::CWord(const CString& rhs) :
  CString(rhs)
{
  // Nothing needed here
}

Notice the colon (:), which when used in the constructor signifies the beginning of the initialization list. For more information about initializing base classes, read about it in any C++ book.
0
 
LVL 2

Author Comment

by:graber
ID: 1327369
could this be accomplish using the assignment operator also?
CWord::CWord(const CString & stringSrc)
{
   CString::operator=(stringSrc);
}
0
 
LVL 2

Author Comment

by:graber
ID: 1327370


0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
In this post we will learn different types of Android Layout and some basics of an Android App.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question