passing class object to pure virtual fkt

Discussion in 'C++' started by Gernot Frisch, Sep 8, 2004.

  1. I want to have a class that provides 2 methods with the same name
    (do1, do2) that cann be called from a function (Fkt) but Fkt does not
    know/care about it's type.


    class base
    {
    public:
    virtual void Do(base& b)=0;
    };

    class do1:public base
    {
    public:
    void Do(base& d) {printf("%d", (do1)d.magic);}
    long magic;
    };

    class do2:public base
    {
    public:
    void Do(base& d) {printf("%d", (do1)d.othermagic);}
    long othermagic;
    };


    void Fkt(base& cl)
    {
    cl.Do(cl);
    }


    --
    -Gernot
    int main(int argc, char** argv) {printf
    ("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

    ________________________________________
    Looking for a good game? Do it yourself!
    GLBasic - you can do
    www.GLBasic.com
     
    Gernot Frisch, Sep 8, 2004
    #1
    1. Advertising

  2. Gernot Frisch wrote:
    > I want to have a class that provides 2 methods with the same name
    > (do1, do2) that cann be called from a function (Fkt) but Fkt does not
    > know/care about it's type.
    >


    I must be slow this morning. Do you ming clarifying your question ?


    >
    > class base
    > {
    > public:
    > virtual void Do(base& b)=0;
    > };
    >
    > class do1:public base
    > {
    > public:
    > void Do(base& d) {printf("%d", (do1)d.magic);}
    > long magic;
    > };
    >
    > class do2:public base
    > {
    > public:
    > void Do(base& d) {printf("%d", (do1)d.othermagic);}
    > long othermagic;
    > };
    >
    >
    > void Fkt(base& cl)
    > {
    > cl.Do(cl);
    > }
    >
    >
     
    Gianni Mariani, Sep 8, 2004
    #2
    1. Advertising

  3. Gernot Frisch

    Moonlit Guest

    Hi,

    "Gernot Frisch" <> wrote in message
    news:...
    >I want to have a class that provides 2 methods with the same name (do1,
    >do2) that cann be called from a function (Fkt) but Fkt does not know/care
    >about it's type.
    >
    >
    > class base
    > {
    > public:
    > virtual void Do(base& b)=0;
    > };
    >
    > class do1:public base
    > {
    > public:
    > void Do(base& d) {printf("%d", (do1)d.magic);}
    > long magic;
    > };


    In your posts I always see you pass the base class in the derived object.
    This isn't normally necessary. Don't forget inheritance is NOT a
    child->parent relation ship. The derived object IS the base class and the
    derived class. so that would make the Do function:

    #include <iostream>
    class base
    public:
    virtual void Do()=0;
    };
    class do1:public base
    {
    public:
    virtual void Do() {std::cout << magic << std::endl; }
    long magic;
    };
    class do2:public base
    {
    public:
    virtual void Do() {std::cout << othermagic << std::endl;}
    long othermagic;
    };

    (The virtual in the derived has actual no effect but it, shows that the
    function is a virtual to anyone reading the code).


    >
    > class do2:public base
    > {
    > public:
    > void Do(base& d) {printf("%d", (do1)d.othermagic);}
    > long othermagic;
    > };
    >
    >
    > void Fkt(base& cl)
    > {
    > cl.Do(cl);
    > }
    >
    >
    > --
    > -Gernot
    > int main(int argc, char** argv) {printf ("%silto%c%cf%cgl%ssic%ccom%c",
    > "ma", 58, 'g', 64, "ba", 46, 10);}
    >
    > ________________________________________
    > Looking for a good game? Do it yourself!
    > GLBasic - you can do
    > www.GLBasic.com
    >
    >
     
    Moonlit, Sep 8, 2004
    #3
  4. Oh. My problem is another:
    I don't want to pass 'this' to the function, but another instance of
    the class. Such as:

    class Crayons : public ColorfulPens
    {
    void SetMeTheColorOf(const Crayon& anotherCrayon);
    }

    Is it clearer now?
    Sorry for being unable to express my problems.
    -Gernot
     
    Gernot Frisch, Sep 9, 2004
    #4
  5. Gernot Frisch

    Moonlit Guest

    Hi,

    I suppose you want to do this and set Crayons color also from ColorfullPens
    (I also assume the Crayon in the function should be Crayons ),.

    class CColor{};
    class ColorfulPens
    {

    CColor Color;
    public:
    const CColor& GetColor() const { return Color; }
    void SetColor( const CColor& Color ) { this->Color = Color; }
    };
    class Crayons : public ColorfulPens
    {
    public:
    void SetMeTheColorOf(const ColorfulPens& Pen )
    {
    SetColor( Pen.GetColor(); }
    }
    };

    Regards, Ron AF Greve

    "Gernot Frisch" <> wrote in message
    news:...
    > Oh. My problem is another:
    > I don't want to pass 'this' to the function, but another instance of the
    > class. Such as:
    >
    > class Crayons : public ColorfulPens
    > {
    > void SetMeTheColorOf(const Crayon& anotherCrayon);
    > }
    >
    > Is it clearer now?
    > Sorry for being unable to express my problems.
    > -Gernot
    >
     
    Moonlit, Sep 9, 2004
    #5
  6. "Moonlit" <news moonlit xs4all nl> schrieb im Newsbeitrag
    news:413ffecd$0$21106$4all.nl...
    > Hi,
    >
    > I suppose you want to do this and set Crayons color also from
    > ColorfullPens (I also assume the Crayon in the function should be
    > Crayons ),.
    >
    > class CColor{};
    > class ColorfulPens
    > {
    >
    > CColor Color;
    > public:
    > const CColor& GetColor() const { return Color; }
    > void SetColor( const CColor& Color ) { this->Color = Color; }
    > };
    > class Crayons : public ColorfulPens
    > {
    > public:
    > void SetMeTheColorOf(const ColorfulPens& Pen )
    > {
    > SetColor( Pen.GetColor(); }
    > }
    > };


    Now, make

    class WaxCrayon:public ColorFulPens
    {
    public:
    void SetMeTheColorOf(const ColorfulPens& Pen)
    {
    // now please cast Pen to type: WaxCrayon to access other
    member functions
    }
    };


    I'll explain my situation in detail, so it might give a better
    approach:

    I have a parser class that converts:
    a=3+3/6

    to:

    3
    a=3+-
    6

    In ASCII. Now I want to do the same using e.g. GDI drawings, for nicer
    displaying of roots e.g.

    The class ASCIIblock has these methods:

    void Set(const char* singleline);
    void InsertAt(long x, long y, const ASCIIblock& block_to_paste);
    void MakeDivision(const ASCIIblock& divident, const ASCIIblock&
    divisor);
    ....

    The parser class has a function:

    template class <DRAWblock>bool EvaluateGFX(DRAWblock& out)
    {
    ...
    DRAWblock block1, block2;
    block2.Set(someline);
    block1.InsertAt(0,0, block2);
    ...
    }

    and get's called with:
    ASCIIblock ab;
    MyClass.EvaluateGFX(ab);

    But I need a class that has these functions:
    class GDIblock
    {
    public:
    void Set(const char* singleline);
    void InsertAt(long x, long y, const GDIblock& block_to_paste);
    void MakeDivision(const GDIblock& divident, const GDIblock&
    divisor);
    };

    ....er... wait... that's working isn't it? I have to re-think about my
    problem myself now... (If it still exists)

    -Gernot
     
    Gernot Frisch, Sep 9, 2004
    #6
  7. Gernot Frisch

    Moonlit Guest

    Hi,

    I think I understand your problem better now. So you have a parser class
    that can draw itself on the console by passing it a "console" object. Now
    you want to let the parser class draw in a window so you want to pass it a
    GDI object. Actually what you would like to do is pass the parser class some
    generic interface to draw blocks, characters lines etc. Only the
    implentation (in the derived class) knows how it is going to do that i.e. on
    a ASCII screen or in a GDI context (which in itself is an abstraction for
    printer, screen etc.) or anything else hence:


    class CContext
    {
    public:
    virtual void DrawText() = 0;
    .... DrawLine etc.

    };
    class CGDIContext
    {
    private:
    HDC DC;
    public:
    CGDIContext( HDC DC ):
    DC( DC){}

    virtual void DrawText()
    {
    // use TextOut etc to draw tree in device context
    }
    .... DrawLine etc.
    };
    class CASCIIContext
    {
    public:
    virtual void DrawText()
    {
    // Use printf to draw tree on console
    }
    .... DrawLine etc.
    }

    class CParser
    {
    private:
    CParseTreee Tree;
    public:
    void DrawMyself( CContext& Context )
    {
    // Draw the tree using the abstract line text etc of Context.
    Context.DrawText( Tree.GetNode() ); // or something like that
    }
    };

    CParser Parser;

    Parser.Parse( file);
    CGDIContext GDIContext( GetWindowDC() );
    Paser.DrawMyself( GDIContext );
    CASCIIContext ASCIIContext;
    Paser.DrawMyself( ASCIIContext );


    Regards, Ron AF Greve.

    you want to make that parser class a
    "Gernot Frisch" <> wrote in message
    news:...
    >
    > "Moonlit" <news moonlit xs4all nl> schrieb im Newsbeitrag
    > news:413ffecd$0$21106$4all.nl...
    >> Hi,
    >>
    >> I suppose you want to do this and set Crayons color also from
    >> ColorfullPens (I also assume the Crayon in the function should be
    >> Crayons ),.
    >>
    >> class CColor{};
    >> class ColorfulPens
    >> {
    >>
    >> CColor Color;
    >> public:
    >> const CColor& GetColor() const { return Color; }
    >> void SetColor( const CColor& Color ) { this->Color = Color; }
    >> };
    >> class Crayons : public ColorfulPens
    >> {
    >> public:
    >> void SetMeTheColorOf(const ColorfulPens& Pen )
    >> {
    >> SetColor( Pen.GetColor(); }
    >> }
    >> };

    >
    > Now, make
    >
    > class WaxCrayon:public ColorFulPens
    > {
    > public:
    > void SetMeTheColorOf(const ColorfulPens& Pen)
    > {
    > // now please cast Pen to type: WaxCrayon to access other member
    > functions
    > }
    > };
    >
    >
    > I'll explain my situation in detail, so it might give a better approach:
    >
    > I have a parser class that converts:
    > a=3+3/6
    >
    > to:
    >
    > 3
    > a=3+-
    > 6
    >
    > In ASCII. Now I want to do the same using e.g. GDI drawings, for nicer
    > displaying of roots e.g.
    >
    > The class ASCIIblock has these methods:
    >
    > void Set(const char* singleline);
    > void InsertAt(long x, long y, const ASCIIblock& block_to_paste);
    > void MakeDivision(const ASCIIblock& divident, const ASCIIblock& divisor);
    > ...
    >
    > The parser class has a function:
    >
    > template class <DRAWblock>bool EvaluateGFX(DRAWblock& out)
    > {
    > ...
    > DRAWblock block1, block2;
    > block2.Set(someline);
    > block1.InsertAt(0,0, block2);
    > ...
    > }
    >
    > and get's called with:
    > ASCIIblock ab;
    > MyClass.EvaluateGFX(ab);
    >
    > But I need a class that has these functions:
    > class GDIblock
    > {
    > public:
    > void Set(const char* singleline);
    > void InsertAt(long x, long y, const GDIblock& block_to_paste);
    > void MakeDivision(const GDIblock& divident, const GDIblock& divisor);
    > };
    >
    > ...er... wait... that's working isn't it? I have to re-think about my
    > problem myself now... (If it still exists)
    >
    > -Gernot
    >
    >
    >
    >
    >
    >
    >
     
    Moonlit, Sep 9, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. IK
    Replies:
    2
    Views:
    612
    hemraj
    Jul 23, 2004
  2. John Goche
    Replies:
    10
    Views:
    753
    Marcus Kwok
    Dec 8, 2006
  3. Replies:
    7
    Views:
    589
    James Kanze
    May 2, 2007
  4. a
    Replies:
    7
    Views:
    364
    dasjotre
    Jun 28, 2007
  5. Replies:
    4
    Views:
    457
Loading...

Share This Page