 |
Visual C++ Questions specific to Microsoft's Visual C++. For questions not specific to this Microsoft version, use the C++ Programming forum instead. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Visual C++ section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

August 26th, 2004, 10:34 PM
|
Registered User
|
|
Join Date: Aug 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Calling base class assignment function
I am a beginner at C++. I am trying to derive a class from the MFC CString class to add additional functions. It appears that I have to supply all of the constructor and assignment functions that are defined in the original CString class to maintain total compatibility. I have overridden the constructor functions and basically just call the base class constructor functions.
However, I am having trouble determining how I can call the base class assignment functions from the assignment functions in my derived class. Any help would be appreciated.
|

August 27th, 2004, 12:59 PM
|
Registered User
|
|
Join Date: Aug 2004
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
If you can post your source, I might be able to give you some suggestions.
|

August 27th, 2004, 09:22 PM
|
Registered User
|
|
Join Date: Aug 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
All I need to do is add new functions to the CString class. I do not need to add any data members or override any of the current functions. However, since the constructor, destructor and assignment functions are not inherited in a derived class, I have to define all of these in my derived class. Therefore, as an example the following is one of the constructor functions that I have defined:
CGString( TCHAR ch, int nRepeat ) :CString( ch, nRepeat ) {}
My question is how do I do this for the assignment functions in the CSTring class. For example, how would I code replacements for the following CString assignment functions. That is how do I call the respective base class functions.
CGString( const CGString& operator =( const CGString& stringSrc )
const CGString& operator =( LPCSTR lpsz )
The derived class definition is as follows:
class AFX_EXT_CLASS CGString: public CString
{
public:
// Constructors
CString( );
CGString( const CGString& stringSrc );
CGString( TCHAR ch, int nRepeat = 1 );
CGString( LPCSTR lpsz );
CGString( LPCWSTR lpsz );
CGString( LPCTSTR lpch, int nLength );
CString( const unsigned char* psz );
// Destructor
~CGString();
// Assignment operators
CGString( const CGString& operator =( const CGString& stringSrc );
const CGString& operator =( TCHAR ch );
const CGString& operator =( char ch );
const CGString& operator =( LPCWSTR lpsz );
const CGString& operator =( LPCSTR lpsz );
const CGString& operator =( const unsigned char* psz );
// New member functions
o o o o
};
|

August 28th, 2004, 01:41 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Are you making an MFC extension DLL or just want an ad-hoc in-application type class derived from CString.
|

August 28th, 2004, 12:24 PM
|
Registered User
|
|
Join Date: Aug 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I am writing a MFC extension DLL.
|

September 19th, 2004, 08:40 PM
|
Registered User
|
|
Join Date: Aug 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
So does no one know the answer to this question or is my question not clear. When a class is derived from another class, the assignment functions of the base class are not inherited. Yet if there are private data members, the base class assignment function must be called to set these private data members. How do I do this in a function defined in the derived class?
|

September 22nd, 2004, 07:33 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
|
|
define constructors like
CGString( TCHAR ch, int nRepeat = 1 ):CString(ch,nRepeat) {}
CGString( LPCSTR lpsz ):CString(lpsz) {}
CGString( LPCWSTR lpsz ):CString(lpsz) {}
.
.
.
for various data types that a CString accepts.
It may solve some of your problems.
|

September 22nd, 2004, 09:25 AM
|
Registered User
|
|
Join Date: Aug 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Ankur, thank you for your response but my question is about the assignment operators. How to call the base class constructors from the derived class is covered in Ivor Horton's book. However, I have not found in his book how to call the base class assignment operator functions from the derived class assignment functions.
Here are two function prototype assignment operators for base class CString. If I do not need to change these in my derived class, how would I define the equivalent functions in my derived class. I have to define them because assignment operator functions are not inherited.
CString( const CString& operator =(const CString& stringSrc);
const CString& operator =(TCHAR ch) : CString& operator =(TCHAR ch);
|

September 23rd, 2004, 02:33 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
|
|
When you try to assign values to a perticular type by an arrangement an appropriate contructor in the hirarchy is called to look after the assignment.
If you define the constructors that call the base class contructor as I've suggested your assignment operators would be taken care of.
e.g. Define this constructor
CGString( const CGString& stringSrc ):CString(stringSrc){}
and you would be able to do these kind of assignments
CGString ss;
CString ssd = "asdfsaf";
ss = ssd;
Do let me know I'm thinking on the right lines.
|
|
 |