CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Polymorphic not?

    I have the following piece of code:
    Code:
    CLocation* CLocation::CreateNewLocation( ... ) // Static Function
    {
        // May return pointers to different classes derived from
        // CLocation, but for this example:
        return new CShapeLocation();
    }
    ...
    // Main Code
    CLocation* pLocation = CLocation::CreateNewLocation( ... );
    pLocation->DoPropertiesDlg() ;
    But...
    CLocation:: DoPropertiesDlg()

    is ALWAYS called, not..

    CShapeLocation:: DoPropertiesDlg()

    as I would have expected. What am I doing wrong?

    Thanks
    Rob.

  2. #2
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    You neglected to produce the most important piece of code: the
    declaration of the DoPropertiesDlg() function. Is it possible that
    you forgot to declare the DoPropertiesDlg() function virtual? If
    you did not, you'd better make the destructor virtual as well.

    --Paul

  3. #3
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686
    Yes, it is declared virtual:

    Code:
    virtual bool DoPropertiesDlg( ... ) { ASSERT(false); return false; };

  4. #4
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686
    I found what my problem was.

    The derived function was not declared EXACTLY the same as the base one. One of the parameters in the base function was a CLocation*. In the derived I had declared it as CShapeLocation*. My mistake.

    Thanks
    Rob.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured