How to derive a class from CString

I want to derive a class from CString so I can add functionality to it (such as FindAfter(index), TruncateBefore(CString), etc).  I have created the drived class, but can't figure out how to effectively override the operator=() function so as to let the parent class handle the string.

So, how do I effectively derive a class from CString?
DenManAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nietodCommented:
answer coming.
0
nietodCommented:
The following example shows a derived class with an extra integer member field.  The assignment operator copies this member as well as the string base class.

class DerStr : public cString
{
 int AdditionalMember;
}

DerStr & DerStr::operator = (const DerStr &Src)
{
   *(cString) this = Src; // Assign the base classes.
   AdditionalMember = Src.AdditionalMember;
   return this;
};

I hope this helps.  Ask if you have questions.
0
nietodCommented:
Opps. The function should return "*this", not just "this".
0
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

DenManAuthor Commented:
That helps a lot.  However, I do have an additional question.

How do I expand this to allow me to the following?

CString vString;
DerStr vDerStr;
vDerStr = vString;

0
RONSLOWCommented:
The answer was really incomplete as you will need constructors and probably some casting operators (eg if you need to go automatically from a DerStr to an LPCTSTR - C++ only auto-casts to a single level not the two levels required DerStr->CString->LPCTSTR)

eg. to be able to copy from a string and cast to an LPCTSTR you'd need:


class DerStr : public CString {
  ...
  DerStr(const CString& s) : CString(s), AdditionalMember(0) { }
  operator LPCTSTR () const { return (LPCTSTR)(CString)*this; }
  ...
};

also for operator= it is usually good practice to check that you are not assigning to yourself.

Also the operator= supplied is wrong anyway as you need to retur n *this instead.

DerStr& DerStr::operator= (const DerStr &Src)
{
   if (this != &src) {
       *(cString) this = Src; // Assign the base classes.
       AdditionalMember = Src.AdditionalMember;
   }
   return *this;
};

I hope this better answers your question (if particularly helpful, maybe some points would be nice... :-)

Roger

0
DenManAuthor Commented:
Thanks, Ronslow, that did help.  However, I'm still having difficulty with the operator= function.  It tells me that it cannot make the conversion from "class CMyString *const to class CString".  Here's my code.


CMyString & CMyString::operator = (const CMyString &Src)
  {
      if (this != &Src) {
            *(CString) this = Src; // Assign the base classes.
      }
     return *this;
  };
0
nietodCommented:
Sorry, I thought you specifically wanted help with the assignment operator--not the entire process.

Is the error on the "if (this != $Src)" line?


0
nietodCommented:
I got it.  Should be.
CMyString & CMyString::operator = (const CMyString &Src)
        {
      if (this != &Src) {
      *(CString *) this = Src; // Assign the base classes.
      }
           return *this;
        };
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nietodCommented:
That probably looks exactly the same to you, doesn't it?  The difference is one '*' added on the line
*(CString *)this = Src;

you were converting this to a CString, not a CString *.  I wonder where you got that lousy code...
0
RONSLOWCommented:
If you have a constructor that builds a CMyString from a CString, then that should allow the operator= given (with the bug fixed) to work OK.

CMyString & CMyString::operator = (const CMyString &Src)
{
  if (this != &Src) {
    *(CString*)this = Src; // Assign the base classes.
    // don't foget to assign your own vars here (if any)
  }
  return *this;
}

CMyString::CMyString(const CString& s)
: CString(s)
// don't forget to init your vars here (if any)
{
  // nothing else to do really
}


Now if you say

CMyString x
CString y;
x = y;

C++ should auto-cast y from CString to CMyString (using the constuctor) and than call the CMyString::operator=

y = x;

C++ will downcast CMyString to a CString (because it IS a CString) and use the CString::operator==

Alternatively, you could provide an additional operator=

CMyString & CMyString::operator = (const CString &Src)
{
  if (this != &Src) {
    *(CString*)this = Src; // Assign the base classes.
    // don't foget to do something with your vars if any
  }
  return *this;
}

NOTE: arg for this operator= is a CString and not a CMyString.

Sorry I cannot post this as an answer, nietod beat me again !!

0
nietodCommented:
The RONSLOW vx NIETOD debates part II ... just when you thought it was safe to answer...

Actually, since you have a CMyString constructor that takes only a constant CString.  The compiler will use this whenever it needs to convert a CString to a CMyString.  Because of this it will never call the operator = that takes a CString.  Instead it will convert the CString to a CMyString with the constructor and then use the regular assignment operator.  (That seems like a bad design, but that is the way it works.)  To get around this you can declare the constructor as "explicit" and it will no longer be invoked for implicit conversions.

Round 2?
0
RONSLOWCommented:
nietod -- I said that the operator= was AN ALTERNATIVE to having a constructor from CString.

If you have the constructor from CString, then you don't need an additioal operator=.

Even if you DO have both. the additional operator= that takes a CString, then that would be called rather than doing the cast as it is a better match.  C++ prefers exact matches in function args to having to do a cast.

I hadn't realised that VC now support "explicit".  I'll have to start using it now !!! (THANKS FOR THE INFO)

Unfortunately, making the CString cast explicit will stop it being used where we want it to be .. we WANT implict cast to from CString to CMyString.


Round 3?

0
RONSLOWCommented:
I would have LIKED it to be safe to answer .. but you keep beating me to it :-( .. Thems the breaks, I suppose.

Our fate lies in the hands of the gods (well .. DenMan anyway)

0
DenManAuthor Commented:
Both of you have been helpful (although the specific line of
    *(CString *)this = Src
took both a couple posts to get completely accurate.)      ;)

I would like to reward both for your efforts...suggestions on how to do that?
0
RONSLOWCommented:
Have a look at the loung area .. I think there are ideas there about point splitting.  Also in the suggestions area.

0
nietodCommented:
Well, now that you mention it,  I do see the "alternately"  However, I find that explicit constructors cause too many problems.  I prefer explicit, even if it means you have to redefine a lot of other functions.

DenMan, The more people complain about the inability to split points the sooner it will be that we can do so. . .
0
nietodCommented:
If you have points to spare.  Some people post "empty" questions for specific experts to reward them for their efforts when they we're the answerers.
0
RONSLOWCommented:
send a message to Linda and she will work it out.

Linda@Experts-Exchange.com (I think)

Or post a question in the Experts-Exchange customer service topic.

What happens is either you can apply to have the number of points reduced and then accept an answer here and post another question marked "For RONSLOW only" and I can answers it to get the other part of the points.  Or Linda may credit your account with extra points so we can both get full points.  Or you can give us both full points (if you can afford it and think we deserve it).

See what Linda suggests.


0
nietodCommented:
Splitting the points is fine by me.  But I'm not sure Linda likes to do that sort of stuff.  Doesn't hurt to ask.  (And I suppose that asking her a lot will definitly encourage putting the feature in!)
0
DenManAuthor Commented:
I'm just going to post a second question for points for RONSLOW.  Thanks to both.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.