Solved

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

Posted on 2010-11-11
7
716 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
Independent Software Vendors: 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

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Before we dive into the marketing strategies involved with creating an effective homepage, it’s crucial that EE members know what a homepage is. In essence, a homepage is the introductory, or default page, of a website that typically highlights the …
The viewer will learn how to synchronize PHP projects with a remote server 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++.

734 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