Squirrel

The programming language
Welcome to Squirrel Sign in | Join | Help
in Search

[SqPlus] Returning a pointer to an instance of a derived class

Last post 07-01-2007, 7:39 AM by Katsuaki Kawachi. 7 replies.
Sort Posts: Previous Next
  •  06-08-2007, 5:08 AM 1875

    [SqPlus] Returning a pointer to an instance of a derived class

    Hello everyone,

    I'm not sure if this has been discussed before, even though I did a search before posting. Anyway here's the long and short of it.

    First the short:
    Whenever I try returning a pointer to an instance of a derived class I cannot call the base classes members on the returned object.

    Permit me to demonstrate by means of the long:

    C++ code:
    ~~~~~~~~
    class cBase
    {
    public:
        cBase() {}
        virtual ~cBase() {}

        const SQChar *Name () {return "cBase";}
        const SQChar *BaseSpecific () {return "Base Specific";}
    };

    class cDerived : public cBase
    {
    public:
        cDerived() {}
        virtual ~cDerived() {}

        const SQChar *Name () {return "cDerived";}
        const SQChar *DerivedSpecific () {return "Derived Specific";}
    };

    DECLARE_INSTANCE_TYPE(cBase)
    DECLARE_INSTANCE_TYPE(cDerived)

    cBase b;
    cDerived d;

    cBase &GetBase() {return b;}
    cDerived &GetDerived() {return d;}
    cBase *GetBasePtr() {return &b;}
    cDerived *GetDerivedPtr() {return &d;}

    ...

    SQClassDef<cBase>(_T("cBase"))
        .func(&cBase::Name, _T("Name"))
        .func(&cBase::BaseSpecific, _T("BaseSpecific"));

    SQClassDef<cDerived>(_T("cDerived"), _T("cBase"))
        .func(&cDerived::Name, _T("Name"))
        .func(&cDerived::DerivedSpecific, _T("DerivedSpecific"));

    RegisterGlobal(&GetBase, _T("GetBase"));
    RegisterGlobal(&GetDerived, _T("GetDerived"));
    RegisterGlobal(&GetBasePtr, _T("GetBasePtr"));
    RegisterGlobal(&GetDerivedPtr, _T("GetDerivedPtr"));

    Squirrel Code:
    ~~~~~~~~~~~
    b <- cBase();
    print(b.Name() + "\n");
    print(b.BaseSpecific() + "\n");
    print("b <- cBase() worked perfectly...\n\n");

    b <- GetBase();
    print(b.Name() + "\n");
    print(b.BaseSpecific() + "\n");
    print("b <- GetBase() worked perfectly...\n\n");

    b <- GetBasePtr();
    print(b.Name() + "\n");
    print(b.BaseSpecific() + "\n");
    print("b <- GetBasePtr() worked perfectly...\n\n");

    d <- cDerived();
    print(d.Name() + "\n");
    print(d.DerivedSpecific() + "\n");
    print(d.BaseSpecific() + "\n");
    print("d <- cDerived() worked perfectly...\n\n");

    d <- GetDerived();
    print(d.Name() + "\n");
    print(d.DerivedSpecific() + "\n");
    print(d.BaseSpecific() + "\n");
    print("d <- GetDerived() worked perfectly...\n\n");

    d <- GetDerivedPtr();
    print(d.Name() + "\n");
    print(d.DerivedSpecific() + "\n");
    print("but now it's about to fall apart...\n");
    print(d.BaseSpecific() + "\n");

  •  06-08-2007, 9:03 PM 1877 in reply to 1875

    Re: [SqPlus] Returning a pointer to an instance of a derived class

  •  06-08-2007, 11:21 PM 1878 in reply to 1877

    Re: [SqPlus] Returning a pointer to an instance of a derived class

    Yes, thank you very much, it was the problem and the proposed solution works with MinGW GCC.

    Why hasn't the fix been incorporated into the official distribution yet?

    Thanks again,
    nix
  •  06-17-2007, 12:15 AM 1896 in reply to 1878

    Re: [SqPlus] Returning a pointer to an instance of a derived class

    Thank you for your report.  Could you tell me the version of sqplus you've applied the patch to?

    > Why hasn't the fix been incorporated into the official distribution yet?

    Because we have no organized bug tracking system for SqPlus, it is very
    difficult to find the effective patch.  I'm attempting to pick patches from wiki
    or forums and to put into the snapshot, but it's still underway.

    K. Kawachi

  •  06-18-2007, 12:35 AM 1899 in reply to 1896

    Re: [SqPlus] Returning a pointer to an instance of a derived class

    I initially applied the patch to the 20070304 snapshot. Since then I've applied it to the 20070602 snapshot, and everything still seems to work as expected with gcc 3.4.5 (mingw special).


    Thanks again for the pointer Katsuaki, my script bindings have become much simpler since I don't have to try work around this problem anymore.

    Kind regaurds,
    Niki
  •  06-24-2007, 6:26 AM 1914 in reply to 1899

    Re: [SqPlus] Returning a pointer to an instance of a derived class

    Hi Niki,

    I did some tests around the patched versions.  I found that:
    • The patch works well for your test code with gcc and VS.NET
    • VS.NET (2003 / 2005) can pass your test code without the patch
    • Tests in "testSqPlus2unit/test_ClassInstance.cpp" fails with the patch
    So, for gcc I've made small modification to the original snapshot.
    I also added your test code to testSqPlus2unit/test_PointerToDerived.cpp.

    Could you please test this modified version of SqPlus with your application?
    You can get it from subversion:
     $ svn co https://sqplus.svn.sourceforge.net/svnroot/sqplus/branches/kkawachi-r54-pointer-to-derived

    K. Kawachi

  •  06-25-2007, 12:47 AM 1915 in reply to 1914

    Re: [SqPlus] Returning a pointer to an instance of a derived class

    Hi Katsuaki,

    I'm glad to see you cleaned up my messy test before adding it to the distribution Stick out tongue [:P]

    I've built and tested your latest commit with no ill effects. A few quick tests of my bindings and everything seems to work nicely.

    Great job! Thank you,
    Niki
  •  07-01-2007, 7:39 AM 1925 in reply to 1915

    Re: [SqPlus] Returning a pointer to an instance of a derived class

    I merged the fix into trunk and updated the snapshot of sqplus (20070701).
     http://wiki.squirrel-lang.org/default.aspx/SquirrelWiki/SqPlus.html

    Many thanks!
View as RSS news feed in XML
Powered by Community Server, by Telligent Systems