Executing function from super class to inherited class.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Snyke

    #1

    Executing function from super class to inherited class.

    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::OnComm and is executed
    and not Socket2::OncCom mand.

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

    Snyke
  • Victor Bazarov

    #2
    Re: Executing function from super class to inherited class.

    Snyke wrote:[color=blue]
    > 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::OnComm and is executed
    > and not Socket2::OncCom mand.
    >
    > Anybody got an idea why? Or did I misunderstand something.[/color]

    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

    Comment

    • John Harrison

      #3
      Re: Executing function from super class to inherited class.


      "Snyke" <snyke@counters trike.de> wrote in message
      news:e1b6c1a9.0 409301351.22668 38d@posting.goo gle.com...[color=blue]
      > 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::OnComm and is executed
      > and not Socket2::OncCom mand.
      >
      > Anybody got an idea why? Or did I misunderstand something.
      >[/color]

      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


      Comment

      • Christian  Decker

        #4
        Re: Executing function from super class to inherited class.

        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?).

        Comment

        • Victor Bazarov

          #5
          Re: Executing function from super class to inherited class.

          Christian Decker wrote:[color=blue]
          > 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?[/color]

          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).
          [color=blue]
          > [...][/color]

          V

          Comment

          • John Harrison

            #6
            Re: Executing function from super class to inherited class.


            "Christian Decker" <Decker.Christi an@gmail.com> wrote in message
            news:1096653227 .309376.181760@ h37g2000oda.goo glegroups.com.. .[color=blue]
            > 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?
            >[/color]

            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.
            [color=blue]
            > 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?).
            >[/color]

            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


            Comment

            • Christian  Decker

              #7
              Re: Executing function from super class to inherited class.

              Thanks alot :)
              I already guessed that virtual had something to do with it...

              Comment

              • Jeff

                #8
                Re: Executing function from super class to inherited class.

                "Christian Decker" <Decker.Christi an@gmail.com> wrote in message news:<109665322 7.309376.181760 @h37g2000oda.go oglegroups.com> ...[color=blue]
                > 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?[/color]

                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. ;-)

                Comment

                • Christian  Decker

                  #9
                  Re: Executing function from super class to inherited class.

                  Yeah, I noticed ;-)

                  Comment

                  Working...