Solved

class CWord:public CString process of assignment education.

Posted on 1999-01-07
6
213 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 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