Solved

How to add a new function to a pre-defined class

Posted on 2010-11-11
7
717 Views
Last Modified: 2013-12-14
I'm using C++ (MS Visual C++ with MFC, but that that's not important).

The compiler has a pre-defined class, CString. I know how to add Func(CString1, CString2), but I'd rather not do it this way.

How would I add a function like CString1.Func(CString2) that uses the predefined class?

0
Comment
Question by:DickStone
[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
7 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 50 total points
ID: 34113869
You can derive from that class to add your method, e.g.
class CMyString : public CString
{
public:

  void Func(CMyString& str);
};

Open in new window

0
 

Author Comment

by:DickStone
ID: 34116147
I tried using that to write a function that compares two strings (will be more complicated later). See below.

However, the compiler says that there is no conversion with (Cstring)this for CMyString to CString. How do I get around this?


class CMyString : public CString
{
public:
   int CompareNoFrench(CString& str){
            return( (CString)this > str );
  }
};
 
0
 
LVL 3

Expert Comment

by:xpero
ID: 34116726
i think there is a source code for MFC classes. download it and modify the class you  want.
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 16

Expert Comment

by:HooKooDooKu
ID: 34116931
Try defining the (CString) conversion operator with you CMyString class.  Try something like this:

CMyString::operator CString()
{
    return CString( (LPCTSTR) this );
}

The function will create a CString and initial its contents with the text of what is in CMyString via the already defined operator (LPCTSTR).  Don't forget to declare the operator in the class declaration with something like:
    operator CString();
0
 

Author Comment

by:DickStone
ID: 34120756
Didn't work. See code and error message below.

-----------------------------

class CMyString : public CString
{
public:
      operator CString();
      int CompareNoFrench(CString& str);
};

CMyString::operator CString()
{
      return CString( (LPCTSTR) this );
}

int CMyString::CompareNoFrench(CString& str){
      return( (CString)this > str );
}

-----------------------------

2>c:\wfree\ussalists.cpp(60): error C2440: 'type cast' : cannot convert from 'CMyString *const ' to 'CString'
2>          No constructor could take the source type, or constructor overload resolution was ambiguous
0
 
LVL 16

Accepted Solution

by:
HooKooDooKu earned 200 total points
ID: 34121061
Here's what I could get to compile... with no 'type cast' operators needing to be defined.

int CMyString::CompareNoFrench( CString& str )
{
    return( (CString&) *this > str );
}
0
 

Author Closing Comment

by:DickStone
ID: 34125436
Thanks for sticking with it until it worked.
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

In this article you will learn how to create a free basic website on Bitbucket, a git service provider. Polymer creates dynamic HTML components, which allow more flexibility than static HTML. This tutorial uses Ubuntu Linux but can also be done on W…
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 viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

636 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