?
Solved

How to derive a class from CString

Posted on 1998-04-23
20
Medium Priority
?
376 Views
Last Modified: 2010-04-10
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?
0
Comment
Question by:DenMan
[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
  • 10
  • 6
  • 4
20 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1163622
answer coming.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1163623
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
 
LVL 22

Expert Comment

by:nietod
ID: 1163624
Opps. The function should return "*this", not just "this".
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:DenMan
ID: 1163625
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
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1163626
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
 

Author Comment

by:DenMan
ID: 1163627
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
 
LVL 22

Expert Comment

by:nietod
ID: 1163628
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
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 1163629
I got it.  Should be.
CMyString & CMyString::operator = (const CMyString &Src)
        {
      if (this != &Src) {
      *(CString *) this = Src; // Assign the base classes.
      }
           return *this;
        };
0
 
LVL 22

Expert Comment

by:nietod
ID: 1163630
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
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1163631
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
 
LVL 22

Expert Comment

by:nietod
ID: 1163632
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
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1163633
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
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1163634
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
 

Author Comment

by:DenMan
ID: 1163635
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
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1163636
Have a look at the loung area .. I think there are ideas there about point splitting.  Also in the suggestions area.

0
 
LVL 22

Expert Comment

by:nietod
ID: 1163637
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
 
LVL 22

Expert Comment

by:nietod
ID: 1163638
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
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1163639
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
 
LVL 22

Expert Comment

by:nietod
ID: 1163640
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
 

Author Comment

by:DenMan
ID: 1163641
I'm just going to post a second question for points for RONSLOW.  Thanks to both.
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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

770 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