Executing function from super class to inherited class.

S

Snyke

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
 
V

Victor Bazarov

Snyke said:
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
 
J

John Harrison

Snyke said:
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
 
C

Christian Decker

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

Victor Bazarov

Christian said:
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
 
J

John Harrison

Christian Decker said:
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
 
C

Christian Decker

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

Jeff

Christian Decker said:
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. ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top