Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2010-11-11
7
Medium Priority
?
718 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 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

721 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