Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > Visual C++
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 26th, 2004, 10:34 PM
Registered User
 
Join Date: Aug 2004
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.

Reply With Quote
  #2 (permalink)  
Old August 27th, 2004, 12:59 PM
Registered User
 
Join Date: Aug 2004
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If you can post your source, I might be able to give you some suggestions.

Reply With Quote
  #3 (permalink)  
Old August 27th, 2004, 09:22 PM
Registered User
 
Join Date: Aug 2004
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
  };


Reply With Quote
  #4 (permalink)  
Old August 28th, 2004, 01:41 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma
Default

Are you making an MFC extension DLL or just want an ad-hoc in-application type class derived from CString.
Reply With Quote
  #5 (permalink)  
Old August 28th, 2004, 12:24 PM
Registered User
 
Join Date: Aug 2004
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am writing a MFC extension DLL.

Reply With Quote
  #6 (permalink)  
Old September 19th, 2004, 08:40 PM
Registered User
 
Join Date: Aug 2004
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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?

Reply With Quote
  #7 (permalink)  
Old September 22nd, 2004, 07:33 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma
Default

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.
Reply With Quote
  #8 (permalink)  
Old September 22nd, 2004, 09:25 AM
Registered User
 
Join Date: Aug 2004
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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);


Reply With Quote
  #9 (permalink)  
Old September 23rd, 2004, 02:33 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma
Default

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.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling Oracle function from Vb.net class monuindia2002 ADO.NET 1 August 4th, 2006 05:00 AM
Calling base constuctor with alternative argument rodmcleay C# 2 January 12th, 2005 08:21 PM
Base Class for Code-Behind Problem groupmatch BOOK: ASP.NET Website Programming Problem-Design-Solution 3 September 8th, 2004 05:06 AM
base class inheritance krunch97 VB.NET 2002/2003 Basics 6 April 23rd, 2004 11:48 AM
Help with DBObject base class projectedNexus BOOK: ASP.NET Website Programming Problem-Design-Solution 1 August 12th, 2003 06:36 PM



All times are GMT -4. The time now is 03:17 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.