Solved

class CWord:public CString process of assignment education.

Posted on 1999-01-07
6
222 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

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

690 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