Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2010-11-11
7
Medium Priority
?
721 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
7 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 200 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
Industry Leaders: 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 800 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

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

885 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