abstract class question

Discussion in 'C++' started by www.brook@gmail.com, Mar 5, 2006.

  1. Guest

    I want to enfore the implementation of the operator overloading eg

    class Interface_
    {
    public:
    virtual void operator = (const Interface &other)=0;
    virtual void operator +=(const Interface &other)=0;
    }
    class A:public Interface_
    {
    public:
    int m_a;
    virtual void operator =(const A &other) {m_a = other.m_a;}
    virtual void operator+=(const A &other){m_a += other.m_a;}
    }

    this will not work, since the interefaces are different, A v.s.
    Interace_
    So a solution I have is to use

    class Interface_
    {
    public:
    virtual void operator = (const Interface *other)=0;
    virtual void operator +=(const Interface *other)=0;
    }
    class A:public Interface_
    {
    public:
    int m_a;
    virtual void operator =(const _Interface *other) {m_a =
    ((A*)(other))->m_a;}
    virtual void operator+=(const _Interface *other){m_a +=
    ((A*)(other))->m_a;}
    };
    But this looks not clean, because of the pointer conversion, it is not
    safe, Is there better solution?

    Thanks in advance!
     
    , Mar 5, 2006
    #1
    1. Advertising

  2. wrote:
    > [...]


    'comp.lang.c++' is not a chat-room, nor is it an IRC channel.
    Post and _wait_ for replies. Patience is a virtue.

    V
    --
    Please remove capital As from my address when replying by mail
     
    Victor Bazarov, Mar 5, 2006
    #2
    1. Advertising

  3. Greg Guest

    wrote:
    > I want to enfore the implementation of the operator overloading eg
    >
    > class Interface_
    > {
    > public:
    > virtual void operator = (const Interface &other)=0;
    > virtual void operator +=(const Interface &other)=0;
    > }
    > class A:public Interface_
    > {
    > public:
    > int m_a;
    > virtual void operator =(const A &other) {m_a = other.m_a;}
    > virtual void operator+=(const A &other){m_a += other.m_a;}
    > }
    >
    > this will not work, since the interefaces are different, A v.s.
    > Interace_
    > So a solution I have is to use
    >
    > class Interface_
    > {
    > public:
    > virtual void operator = (const Interface *other)=0;
    > virtual void operator +=(const Interface *other)=0;
    > }
    > class A:public Interface_
    > {
    > public:
    > int m_a;
    > virtual void operator =(const _Interface *other) {m_a =
    > ((A*)(other))->m_a;}
    > virtual void operator+=(const _Interface *other){m_a +=
    > ((A*)(other))->m_a;}
    > };
    > But this looks not clean, because of the pointer conversion, it is not
    > safe, Is there better solution?


    Why bother? If the overloaded operator is not implemented, than any
    code that would have used the overloaded operator had it been
    implemented, will fail to compile when no overloaded operator is found.


    And if the overloaded operator is not used anywhere in the program, why
    waste time requiring that an implementation be written anyway, even
    though it will not be called?

    Greg
     
    Greg, Mar 6, 2006
    #3
  4. Greg wrote:
    > wrote:
    >> I want to enfore the implementation of the operator overloading eg
    >> [..]

    >
    > Why bother? If the overloaded operator is not implemented, than any
    > code that would have used the overloaded operator had it been
    > implemented, will fail to compile when no overloaded operator is
    > found.
    >
    >
    > And if the overloaded operator is not used anywhere in the program,
    > why waste time requiring that an implementation be written anyway,
    > even though it will not be called?


    If the class does not declare/define a final overrider for a pure
    function, such class cannot be instantiated.

    V
    --
    Please remove capital As from my address when replying by mail
     
    Victor Bazarov, Mar 6, 2006
    #4
  5. Greg Guest

    Victor Bazarov wrote:
    > Greg wrote:
    > > wrote:
    > >> I want to enfore the implementation of the operator overloading eg
    > >> [..]

    > >
    > > Why bother? If the overloaded operator is not implemented, than any
    > > code that would have used the overloaded operator had it been
    > > implemented, will fail to compile when no overloaded operator is
    > > found.
    > >
    > >
    > > And if the overloaded operator is not used anywhere in the program,
    > > why waste time requiring that an implementation be written anyway,
    > > even though it will not be called?

    >
    > If the class does not declare/define a final overrider for a pure
    > function, such class cannot be instantiated.


    Granted, but I'm saying that using an abstract method to force the
    implementation of the overloaded operator is superfluous here. The user
    will be forced to write the routine anyway - if it is at all needed.

    In the event that the overloaded operator is left unimplemented then
    any expression that would use the operator will fail to compile. So to
    be able to build the program successfully, the user will have to
    implement the referenced - but unimplemented - overloaded operator -
    and will have to do so whether or not an abstract class exists that
    compels the same.

    Greg
     
    Greg, Mar 6, 2006
    #5
  6. Guest

    If abstract class is used, error could happen at run time if you use
    pointers.
    eg

    interface_ * a, * b,

    you do not know what actually pointed by a and b
    when you call
    *a += *b,
    if the base class implemented the operator +=, but the subclass did
    not, wired things could happen.
     
    , Mar 7, 2006
    #6
    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. Matthias Kaeppler
    Replies:
    1
    Views:
    471
    R.F. Pels
    May 22, 2005
  2. Sameer
    Replies:
    4
    Views:
    635
    Roedy Green
    Aug 31, 2005
  3. Uzytkownik
    Replies:
    3
    Views:
    610
    Uzytkownik
    Apr 3, 2005
  4. Iyer, Prasad C

    Abstract Methods & Abstract Class

    Iyer, Prasad C, Oct 20, 2005, in forum: Python
    Replies:
    0
    Views:
    552
    Iyer, Prasad C
    Oct 20, 2005
  5. Replies:
    4
    Views:
    866
    Rolf Magnus
    May 17, 2006
Loading...

Share This Page