Solved

How to derive a class from CString

Posted on 1998-04-23
20
335 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
  • 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
 

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 50 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now