Executing function from super class to inherited class.

Discussion in 'C++' started by Snyke, Sep 30, 2004.

  1. Snyke

    Snyke Guest

    Ok this is a bit fishy actually:
    I have a class (a socket wrapper) which reads some data from a stream
    and then should respond to it. This is done in two function (OnRead,
    which reads from the stream, and OnCommand which then decides what to
    do). The proble is that I have an inherited class which overloads the
    OnCommand function (but not OnRead). In some way it looks like the
    non-overloaded one is executed even if the Object is of the inherited
    class.

    Code:
    class Socket1 {
    public:
    ...
    private:
    void OnCommand(string comm);
    void OnRead();
    
    }
    
    class Socket2 {
    private:
    OnCommand(string comm);
    }
    
    int main(int argc,char *argv[]){
    Socket2 s;
    s.go();
    }
    
    The problem as described above is that Socket1::OnCommand is executed
    and not Socket2::OncCommand.

    Anybody got an idea why? Or did I misunderstand something.

    Snyke
     
    Snyke, Sep 30, 2004
    #1
    1. Advertising

  2. Snyke wrote:
    > Ok this is a bit fishy actually:
    > I have a class (a socket wrapper) which reads some data from a stream
    > and then should respond to it. This is done in two function (OnRead,
    > which reads from the stream, and OnCommand which then decides what to
    > do). The proble is that I have an inherited class which overloads the
    > OnCommand function (but not OnRead). In some way it looks like the
    > non-overloaded one is executed even if the Object is of the inherited
    > class.
    >
    >
    Code:
    > class Socket1 {
    >   public:
    >     ...
    >   private:
    >     void OnCommand(string comm);
    >     void OnRead();
    >
    > }
    >
    > class Socket2 {
    >   private:
    >     OnCommand(string comm);
    > }
    >
    > int main(int argc,char *argv[]){
    >   Socket2 s;
    >   s.go();
    > }
    > 
    >
    > The problem as described above is that Socket1::OnCommand is executed
    > and not Socket2::OncCommand.
    >
    > Anybody got an idea why? Or did I misunderstand something.


    The code you posted is not C++. Perhaps if you post the real code
    (trimmed up to remove extraneous stuff), we could diagnose the problem
    better.

    If I had to guess, I'd say that (a) the functions in 'Socket1' are not
    virtual or (b) the 'OnCommand' function in 'Socket2' does not override
    the one in 'Socket1' because it has different arguments (and in your
    posting you made them the same, while in the real code they aren't).
    But that's if I had to guess.

    Victor
     
    Victor Bazarov, Sep 30, 2004
    #2
    1. Advertising

  3. "Snyke" <> wrote in message
    news:...
    > Ok this is a bit fishy actually:
    > I have a class (a socket wrapper) which reads some data from a stream
    > and then should respond to it. This is done in two function (OnRead,
    > which reads from the stream, and OnCommand which then decides what to
    > do). The proble is that I have an inherited class which overloads the
    > OnCommand function (but not OnRead). In some way it looks like the
    > non-overloaded one is executed even if the Object is of the inherited
    > class.
    >
    >
    Code:
    > class Socket1 {
    >  public:
    >    ...
    >  private:
    >    void OnCommand(string comm);
    >    void OnRead();
    >
    > }
    >
    > class Socket2 {
    >  private:
    >    OnCommand(string comm);
    > }
    >
    > int main(int argc,char *argv[]){
    >  Socket2 s;
    >  s.go();
    > }
    > 
    >
    > The problem as described above is that Socket1::OnCommand is executed
    > and not Socket2::OncCommand.
    >
    > Anybody got an idea why? Or did I misunderstand something.
    >


    Were you a java programmer first? In C++ OnRead and OnCommand should be
    declared virtual in Socket1 to get the behaviour you want. At least that's
    my best guess from the limited information provided.

    It always best to provide complete code when posting to this group. You
    don't know where the problem is, so if you post partial code you might miss
    out something crucial. In this case the crucial thing is what class the go
    function is part of.

    john
     
    John Harrison, Oct 1, 2004
    #3
  4. Thank you very much.
    Yes, I was a Java Coder before switching to C++ (and I still have some
    problems...). Why do I have to declare a function in a class as virtual
    when I want to override it in a inherited class?

    The problem is that parts of the code I use are proprietary and
    security relevant (Yes, I too prefer OpenSource but it is not
    applicable in this case due to Licensing) and the Socket Wrapper being
    a very big component of my Program (11 Pages of code...) it's simply
    too big to post in here (or would you review codes of this size, just
    to help me?).
     
    Christian Decker, Oct 1, 2004
    #4
  5. Christian Decker wrote:
    > Thank you very much.
    > Yes, I was a Java Coder before switching to C++ (and I still have some
    > problems...). Why do I have to declare a function in a class as virtual
    > when I want to override it in a inherited class?


    Because that's the only way known to C++ to make the function
    _overridable_. Just a reminder: in Java _all_ member functions
    are virtual (essentially).

    > [...]


    V
     
    Victor Bazarov, Oct 1, 2004
    #5
  6. "Christian Decker" <> wrote in message
    news:...
    > Thank you very much.
    > Yes, I was a Java Coder before switching to C++ (and I still have some
    > problems...). Why do I have to declare a function in a class as virtual
    > when I want to override it in a inherited class?
    >


    Virtual functions have an overhead. So, right or wrong, the C++ philosophy
    is that you shouldn't have to pay for something unless you really want it.

    > The problem is that parts of the code I use are proprietary and
    > security relevant (Yes, I too prefer OpenSource but it is not
    > applicable in this case due to Licensing) and the Socket Wrapper being
    > a very big component of my Program (11 Pages of code...) it's simply
    > too big to post in here (or would you review codes of this size, just
    > to help me?).
    >


    I'm not suggesting that you post your entire program, just that you post a
    complete program. One that you got by cutting down the real program. This
    advice is often given but very few people follow it.

    John
     
    John Harrison, Oct 1, 2004
    #6
  7. Thanks alot :)
    I already guessed that virtual had something to do with it...
     
    Christian Decker, Oct 2, 2004
    #7
  8. Snyke

    Jeff Guest

    "Christian Decker" <> wrote in message news:<>...
    > Thank you very much.
    > Yes, I was a Java Coder before switching to C++ (and I still have some
    > problems...). Why do I have to declare a function in a class as virtual
    > when I want to override it in a inherited class?


    You have to use virtual only when you want to override the method
    *and* then call the derived method through a pointer to the base
    class.

    You have to use the keyword virtual because C++ is not Java. ;-)
     
    Jeff, Oct 2, 2004
    #8
  9. Yeah, I noticed ;-)
     
    Christian Decker, Oct 4, 2004
    #9
    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. Guest

    super.super.super how?

    Guest, Feb 19, 2005, in forum: Java
    Replies:
    24
    Views:
    10,864
    Darryl Pierce
    Feb 24, 2005
  2. Fernando Rodriguez

    Getting the super class via the super() function

    Fernando Rodriguez, Nov 21, 2003, in forum: Python
    Replies:
    2
    Views:
    748
    Bob Willan
    Nov 22, 2003
  3. fi3nd
    Replies:
    0
    Views:
    397
    fi3nd
    Apr 8, 2010
  4. 7stud --
    Replies:
    11
    Views:
    465
    7stud --
    Nov 9, 2007
  5. Matthew Braid

    $class::SUPER as well as $instance::SUPER?

    Matthew Braid, Aug 6, 2004, in forum: Perl Misc
    Replies:
    2
    Views:
    318
    Matthew Braid
    Aug 9, 2004
Loading...

Share This Page