Solved

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

Posted on 2010-11-11
7
704 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 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
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.

757 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

13 Experts available now in Live!

Get 1:1 Help Now