Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Executing function from super class to inherited class.

Reply
Thread Tools

Executing function from super class to inherited class.

 
 
Snyke
Guest
Posts: n/a
 
      09-30-2004
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
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      09-30-2004
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
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      10-01-2004

"Snyke" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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


 
Reply With Quote
 
Christian Decker
Guest
Posts: n/a
 
      10-01-2004
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?).

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      10-01-2004
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
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      10-01-2004

"Christian Decker" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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


 
Reply With Quote
 
Christian Decker
Guest
Posts: n/a
 
      10-01-2004
Thanks alot
I already guessed that virtual had something to do with it...

 
Reply With Quote
 
Jeff
Guest
Posts: n/a
 
      10-01-2004
"Christian Decker" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed) roups.com>...
> 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.
 
Reply With Quote
 
Christian Decker
Guest
Posts: n/a
 
      10-04-2004
Yeah, I noticed

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Accessing super-class methods from super-class fi3nd Java 0 04-08-2010 03:10 PM
'Class.inherited' v. 'inherited' syntax inside Class 7stud -- Ruby 11 11-09-2007 06:45 PM
super.super.super how? Java 24 02-24-2005 10:51 PM
$class::SUPER as well as $instance::SUPER? Matthew Braid Perl Misc 2 08-09-2004 12:05 AM
Getting the super class via the super() function Fernando Rodriguez Python 2 11-22-2003 12:08 AM



Advertisments