Solved

class CWord:public CString process of assignment education.

Posted on 1999-01-07
6
215 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
Technology Partners: 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 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

733 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