Solved

class CWord:public CString process of assignment education.

Posted on 1999-01-07
6
210 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
  • 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 15

Accepted Solution

by:
Tommy Hui earned 100 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now