creating methods for objects

Discussion in 'C++' started by =?iso-8859-1?b?QW5kcuk=?=, Jun 18, 2004.

  1. Hi,

    is it possible to override methods for one specific object, like you can
    do in Ruby? Something like:

    --8<--

    class A {
    public:
    void method();
    }

    void A::method() {
    // do something...
    }

    A x = new A();

    void x::method() {
    // do something else...
    }

    --8<--

    Or anything close to that?

    Thanks,

    Andre'
     
    =?iso-8859-1?b?QW5kcuk=?=, Jun 18, 2004
    #1
    1. Advertising

  2. =?iso-8859-1?b?QW5kcuk=?=

    David Harmon Guest

    On Fri, 18 Jun 2004 16:31:01 -0300 in comp.lang.c++, André
    <> wrote,
    >is it possible to override methods for one specific object, like you can
    >do in Ruby?


    No. You might declare a derived class just for that one object.
    You might use a pointer to a function, or some similar low-level
    hackery.
     
    David Harmon, Jun 18, 2004
    #2
    1. Advertising

  3. =?iso-8859-1?b?QW5kcuk=?=

    Phlip Guest

    André wrote:

    > is it possible to override methods for one specific object, like you can
    > do in Ruby? Something like:
    >
    > --8<--
    >
    > class A {
    > public:
    > void method();
    > }
    >
    > void A::method() {
    > // do something...
    > }
    >
    > A x = new A();
    >
    > void x::method() {
    > // do something else...
    > }
    >
    > --8<--
    >
    > Or anything close to that?


    No.

    C++ was invented to fit the C compiler and linker model, with minimal
    changes to legacy compilers. Each translation unit must contain the same
    definition of each method, just to simplify the linker's requirements.

    Investigate function pointers and method pointers, to let 'x' point to a
    method different from its class's method.

    Why can't you just use Ruby if you need it? C++ is for big, low-level
    systems; not for the average code written today.

    --
    Phlip
    http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces
     
    Phlip, Jun 18, 2004
    #3
  4. =?iso-8859-1?b?QW5kcuk=?=

    JKop Guest

    Untested code, but you'll get the jist.


    class BarnyardAnimal
    {
    private:

    int something;

    static int EatDefault(BarnyardAnimal& me, int food)
    {
    me.something = 7 + food; //Accessing member variable
    }

    public:

    int (*pEat)(BarnyardAnimal&,int);

    int Eat(int food)
    {
    pEat(*this,food);
    }

    BarnyardAnimal(void) : Eat(EatDefault)
    {

    }

    };


    int main(void)
    {
    BarnyardAnimal cow;

    cow.Eat(5); //Default function

    cow.Eat = ReturnFunctionPointer();

    cow.Eat(7); //Calls other function
    }


    -JKop
     
    JKop, Jun 18, 2004
    #4
  5. =?iso-8859-1?b?QW5kcuk=?=

    JKop Guest

    JKop posted:


    > BarnyardAnimal(void) : Eat(EatDefault)
    > {
    >
    > }


    TYPO TYPO TYPO

    BarnyardAnimal(void) : pEat(EatDefault)
    {

    }

    >
    >
    > int main(void)
    > {
    > BarnyardAnimal cow;
    >
    > cow.Eat(5); //Default function
    >
    > cow.Eat = ReturnFunctionPointer();



    TYPO TYPO TYPO

    cow.pEat = ReturnFunctionPointer();


    >
    > cow.Eat(7); //Calls other function
    > }
    >



    -JKop
     
    JKop, Jun 18, 2004
    #5
  6. =?iso-8859-1?b?QW5kcuk=?=

    JKop Guest

    Ignore my last 2 posts. I'll rewrite the whole lot here without typos and
    errors. The following compiles:


    class BarnyardAnimal
    {
    private:

    int something;

    static int EatDefault(BarnyardAnimal& me, int food)
    {
    me.something = 7 + food; //Accessing member variable
    }

    friend int EatOther(BarnyardAnimal&, int);

    public:

    int (*pEat)(BarnyardAnimal&,int);

    int Eat(int food)
    {
    pEat(*this,food);
    }

    BarnyardAnimal(void) : pEat(EatDefault)
    {

    }

    };

    int EatOther(BarnyardAnimal& me, int food)
    {
    ;
    }

    int main(void)
    {
    BarnyardAnimal cow;

    cow.Eat(5); //Default function

    cow.pEat = EatOther;

    cow.Eat(7); //Calls other function
    }



    -JKop
     
    JKop, Jun 18, 2004
    #6
  7. Phlip wrote:

    > C++ is for big, low-level
    > systems; not for the average code written today.



    What's that suppose to mean? I use C++ to build Windows (.NET)
    applications elegantly and efficiently.






    Regards,

    Ioannis Vranos
     
    Ioannis Vranos, Jun 18, 2004
    #7
  8. André wrote:

    > Hi,
    >
    > is it possible to override methods for one specific object, like you can
    > do in Ruby? Something like:
    >
    > --8<--
    >
    > class A {
    > public:
    > void method();
    > }
    >
    > void A::method() {
    > // do something...
    > }
    >
    > A x = new A();
    >
    > void x::method() {
    > // do something else...
    > }
    >
    > --8<--
    >
    > Or anything close to that?
    >
    > Thanks,
    >
    > Andre'




    You can use pointers to member functions, or define other "external"
    functions or so something like:


    class base1
    {
    // ...

    public:
    virtual void method() { ... }
    // ....
    };

    base1 obj1;


    // ...


    class base2: public base1
    {
    public:
    void method() { new definition }
    };


    base2 obj2;






    Regards,

    Ioannis Vranos
     
    Ioannis Vranos, Jun 18, 2004
    #8
  9. =?iso-8859-1?b?QW5kcuk=?=

    Mike Austin Guest

    "Ioannis Vranos" <> wrote in message
    news:cavm1l$1mq1$...
    > Phlip wrote:
    >
    > > C++ is for big, low-level
    > > systems; not for the average code written today.

    >
    >
    > What's that suppose to mean? I use C++ to build Windows (.NET)
    > applications elegantly and efficiently.


    Wrong. You use Managed C++ to build Windows .NET applications.

    Mike Austin
     
    Mike Austin, Jun 18, 2004
    #9
  10. Mike Austin wrote:
    > "Ioannis Vranos" <> wrote in message
    > news:cavm1l$1mq1$...
    >
    >>Phlip wrote:
    >>
    >>
    >>>C++ is for big, low-level
    >>>systems; not for the average code written today.

    >>
    >>
    >>What's that suppose to mean? I use C++ to build Windows (.NET)
    >>applications elegantly and efficiently.

    >
    >
    > Wrong. You use Managed C++ to build Windows .NET applications.


    :) What do you mean? The so called "managed extensions" are
    system-specific extensions for the specific platform. For example for
    X-Windows Applications you can use the QT API. For Windows you can use
    Borland's CLX/VCL.






    Regards,

    Ioannis Vranos
     
    Ioannis Vranos, Jun 18, 2004
    #10
  11. =?iso-8859-1?b?QW5kcuk=?=

    Phlip Guest

    Ioannis Vranos wrote:

    > Mike Austin wrote:


    > > Ioannis Vranos wrote:


    > >>Phlip wrote:


    > >>>C++ is for big, low-level
    > >>>systems; not for the average code written today.


    > >>What's that suppose to mean? I use C++ to build Windows (.NET)
    > >>applications elegantly and efficiently.

    > >
    > > Wrong. You use Managed C++ to build Windows .NET applications.

    >
    > :) What do you mean? The so called "managed extensions" are
    > system-specific extensions for the specific platform. For example for
    > X-Windows Applications you can use the QT API. For Windows you can use
    > Borland's CLX/VCL.


    What /I/ mean is to write a GUI, or a Web site, or a bunch of business
    rules, or glue between enterprise modules, you need a rapid and dynamically
    typed language that stays out of your way.

    C++ is fun and tricky because of all the ways to create undefined behavior.
    Anyone who thinks one can efficiently develop while avoiding all these
    issues is inexperienced with other modern languages. C++ is portable OO
    assembler, and it always puts the needs of the CPU above those of the
    programmer.

    What Mike probably meant is "Managed C++" is not C++ because it uses p-code
    to buffer (but not remove) all those undefined behavior problems.

    --
    Phlip
    http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces
     
    Phlip, Jun 19, 2004
    #11
  12. Phlip wrote:

    >>:) What do you mean? The so called "managed extensions" are
    >>system-specific extensions for the specific platform. For example for
    >>X-Windows Applications you can use the QT API. For Windows you can use
    >>Borland's CLX/VCL.

    >
    >
    > What /I/ mean is to write a GUI, or a Web site, or a bunch of business
    > rules, or glue between enterprise modules, you need a rapid and dynamically
    > typed language that stays out of your way.



    The above does not mean anything in particular.



    > C++ is fun and tricky because of all the ways to create undefined behavior.
    > Anyone who thinks one can efficiently develop while avoiding all these
    > issues is inexperienced with other modern languages. C++ is portable OO
    > assembler, and it always puts the needs of the CPU above those of the
    > programmer.



    C++ is occupies both the high level and low level space. And you can
    choose the level of abstraction you like for your programming. For
    regular applications running on top of an OS, you can state at the high
    level of programming, where C++ has nothing to jealous from other
    languages since it supports 4 paradigms and each one is supported well
    (better than most one-paradigm programming languages out there).

    So for example, instead of using a char * with new[] for strings, better
    use std::string.




    > What Mike probably meant is "Managed C++" is not C++ because it uses p-code
    > to buffer (but not remove) all those undefined behavior problems.



    Check these interesting things about C++ and .NET interaction:

    There check the Visual C++ paragraph:
    http://msdn.microsoft.com/vstudio/productinfo/roadmap.aspx#language


    This will become part of the future C++ standard:
    http://www.ecma-international.org/news/ecma-TG5-PR.htm






    Regards,

    Ioannis Vranos
     
    Ioannis Vranos, Jun 19, 2004
    #12
  13. Ioannis Vranos, Jun 19, 2004
    #13
  14. =?iso-8859-1?b?QW5kcuk=?=

    Phlip Guest

    Ioannis Vranos wrote:

    > Phlip wrote:


    > > What /I/ mean is to write a GUI, or a Web site, or a bunch of business
    > > rules, or glue between enterprise modules, you need a rapid and

    dynamically
    > > typed language that stays out of your way.

    >
    > The above does not mean anything in particular.


    You understand dynamic typing, right?

    > C++ is occupies both the high level and low level space. And you can
    > choose the level of abstraction you like for your programming. For
    > regular applications running on top of an OS, you can state at the high
    > level of programming, where C++ has nothing to jealous from other
    > languages since it supports 4 paradigms and each one is supported well
    > (better than most one-paradigm programming languages out there).
    >
    > So for example, instead of using a char * with new[] for strings, better
    > use std::string.


    Bob Hairgrove wrote [in another thread]:

    > Since the base class destructor [of std::list<>] is
    > not virtual, it is not called when
    > your derived class is destroyed.


    The most important resource to optimize is programmer time.

    C++ always errs in favor of the CPU, not the programmer. In some languages
    all destructors are virtual, and the overhead for each class object is high.
    In C++ it is low. If you are programming a cell phone, use C++.

    --
    Phlip
     
    Phlip, Jun 19, 2004
    #14
  15. Phlip wrote:

    > The most important resource to optimize is programmer time.
    >
    > C++ always errs in favor of the CPU, not the programmer. In some languages
    > all destructors are virtual, and the overhead for each class object is high.
    > In C++ it is low. If you are programming a cell phone, use C++.



    Also it favours space efficiency too. What about a heavy duty, non-cell
    phone, server system where we want to utilize every cycle and byte of it?

    Is writing the keyword "virtual", which is rarely needed in essence,
    really that tiresome?






    Regards,

    Ioannis Vranos
     
    Ioannis Vranos, Jun 19, 2004
    #15
  16. =?iso-8859-1?b?QW5kcuk=?=

    Phlip Guest

    Ioannis Vranos wrote:

    > Phlip wrote:
    >
    > > The most important resource to optimize is programmer time.
    > >
    > > C++ always errs in favor of the CPU, not the programmer. In some

    languages
    > > all destructors are virtual, and the overhead for each class object is

    high.
    > > In C++ it is low. If you are programming a cell phone, use C++.

    >
    > Also it favours space efficiency too. What about a heavy duty, non-cell
    > phone, server system where we want to utilize every cycle and byte of it?


    A great way to write one of those is in a soft language, but then profile
    where the big footprint and the time bottlenecks are. Fix the latter by
    improving its algorithm, then converting it to a lower level language.

    The 80/20 rule says that 80% of your bottlenecks are in 20% of your code, so
    converting 20% of it to C++ will speed you up.

    > Is writing the keyword "virtual", which is rarely needed in essence,
    > really that tiresome?


    If that were the only loophole...

    --
    Phlip
    http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces
     
    Phlip, Jun 20, 2004
    #16
  17. In message <eqZAc.6477$>, Phlip
    <> writes
    >
    >Bob Hairgrove wrote [in another thread]:
    >
    >> Since the base class destructor [of std::list<>] is
    >> not virtual, it is not called when
    >> your derived class is destroyed.


    And he was incorrect, as he acknowledged.

    --
    Richard Herring
     
    Richard Herring, Jun 23, 2004
    #17
  18. "Phlip" <> wrote in message news:<El4Bc.523$>...
    > Ioannis Vranos wrote:
    >
    > > Phlip wrote:
    > >
    > > > The most important resource to optimize is programmer time.
    > > >
    > > > C++ always errs in favor of the CPU, not the programmer. In some

    > languages
    > > > all destructors are virtual, and the overhead for each class object is

    > high.
    > > > In C++ it is low. If you are programming a cell phone, use C++.

    > >
    > > Also it favours space efficiency too. What about a heavy duty, non-cell
    > > phone, server system where we want to utilize every cycle and byte of it?

    >
    > A great way to write one of those is in a soft language, but then profile
    > where the big footprint and the time bottlenecks are. Fix the latter by
    > improving its algorithm, then converting it to a lower level language.


    That would be inpractical. One usually knows where are bottlenecks,
    so, adequatly, project can be divided in soft:)/ hard:) parts, about
    the time when project starts.

    >
    > The 80/20 rule says that 80% of your bottlenecks are in 20% of your code, so
    > converting 20% of it to C++ will speed you up.


    Why converting? This just wastes time and possibly introduces knew
    bugs.


    Greetings, Bane.

    P.S. Once I'v wrote banking application using C++ builder,
    faster then other two guys that used oracle forms. I was finished
    long ago before they finished fighting with their tool in order to met
    user requirements :)
     
    Branimir Maksimovic, Jun 23, 2004
    #18
  19. =?iso-8859-1?b?QW5kcuk=?=

    Phlip Guest

    Branimir Maksimovic wrote:

    > Phlip wrote:


    > > A great way to write one of those is in a soft language, but then profile
    > > where the big footprint and the time bottlenecks are. Fix the latter by
    > > improving its algorithm, then converting it to a lower level language.

    >
    > That would be inpractical. One usually knows where are bottlenecks,
    > so, adequatly, project can be divided in soft:)/ hard:) parts, about
    > the time when project starts.
    >
    > >
    > > The 80/20 rule says that 80% of your bottlenecks are in 20% of your code, so
    > > converting 20% of it to C++ will speed you up.

    >
    > Why converting? This just wastes time and possibly introduces knew
    > bugs.
    >
    > Greetings, Bane.
    >
    > P.S. Once I'v wrote banking application using C++ builder,
    > faster then other two guys that used oracle forms. I was finished
    > long ago before they finished fighting with their tool in order to met
    > user requirements :)


    You probably carefully analyzed requirements, developed an object
    model, and implemented it. Props. (That means "proper respects",
    honestly.)

    I once took a program written in absolutely despicable BASIC, all one
    big function, and re-wrote it as a very small amount of Ruby code.

    http://flea.sourceforge.net

    I did it by converting the BASIC into a form that I could call with
    system(""). Then I put the simplest possible input into that program,
    collected the output, and wrote a test case which forced my Ruby code
    to return the same output.

    When the test failed for the correct reason, I wrote just enough Ruby
    to pass the test. Then I wrote another test requesting the next
    simplest feature, and I got that test to pass.

    As the Ruby code grew, I attended to its design, and refactored to
    develop its object model. While refactoring, I ran all the tests every
    1~10 edits.

    Today that program contains around 100 tests. While developing it, the
    only bugs I found were in the immediate feature I worked on. Adding
    new features did not cause bugs, and refactoring to improve the design
    did not cause bugs. And improved designs resist bugs.

    In my current work I am unfamiliar with the profile of bug risks that
    you fear, but of course in the olden days I would never have
    refactored my modules so aggressively. These days I use test-first
    development to avoid operating a debugger, and am free to change my
    designs at whim, including changing a module's language.

    --
    Phlip
    http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces
     
    Phlip, Jun 24, 2004
    #19
    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. 7stud
    Replies:
    11
    Views:
    750
    Dennis Lee Bieber
    Mar 20, 2007
  2. horos11
    Replies:
    18
    Views:
    494
    Terry Reedy
    Oct 4, 2009
  3. Kenneth McDonald
    Replies:
    5
    Views:
    375
    Kenneth McDonald
    Sep 26, 2008
  4. bizt
    Replies:
    1
    Views:
    268
    Dan Rumney
    Jun 3, 2008
  5. Replies:
    8
    Views:
    331
    Jorgen Grahn
    May 16, 2013
Loading...

Share This Page