468,512 Members | 1,415 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

Virtual function definition

Hi all,

I'm moving my experince from C to C++ and while so I'm facing some
problems with inheritance.

Here is my problem:

class A {
// All share attributes
// ....
virtual void Open(void);
}

class B: public A {
void Open(void);
}

class C: public A {
void Open(int i);
}

There is no problem on defining B. However with C there is. The solution
could be, setting in the parent class another virtual and define it to
NULL on B. But this is not what I like. I know that there is a
possibility of changing the return value on the children, but how can I
do so with the arguments of the function???

Is there any way of doing it???

TA.

Aug 22 '06 #1
13 3259

George wrote:
Hi all,

I'm moving my experince from C to C++ and while so I'm facing some
problems with inheritance.

Here is my problem:

class A {
// All share attributes
// ....
virtual void Open(void);
}

class B: public A {
void Open(void);
}

class C: public A {
void Open(int i);
}

There is no problem on defining B. However with C there is. The solution
could be, setting in the parent class another virtual and define it to
NULL on B. But this is not what I like. I know that there is a
possibility of changing the return value on the children, but how can I
do so with the arguments of the function???

Is there any way of doing it???
Nope. In C++ the argument list (the signature) is considered when the
compiler
chooses which function to call.

So your class C knows to functions:
void Open(int i);
and
void Open(); // better to omit void if it is the only argument
inherited from A

Cheers,
Marc

Aug 22 '06 #2
George wrote:
Is there any way of doing it???
What exactly do you want to do?

There is not a particular problem with your code, it's just a bit weird.
Your _overriding_ a function in B and _overwriting_ a function (actually
the name) in C. A call to A::Open will never call C::Open.

If that's what you want, you're fine. If not, what do you want? Clearly
you can't hope to override a virtual function with different parameter
lists.

Jens
Aug 22 '06 #3
"George" <ge*********@NOSPAMgmx.netwrote in message
news:Xn************************@213.0.184.81...
Hi all,

I'm moving my experince from C to C++ and while so I'm facing some
problems with inheritance.

Here is my problem:

class A {
// All share attributes
// ....
virtual void Open(void);
}

class B: public A {
void Open(void);
}

class C: public A {
void Open(int i);
}

There is no problem on defining B. However with C there is. The solution
could be, setting in the parent class another virtual and define it to
NULL on B. But this is not what I like. I know that there is a
possibility of changing the return value on the children, but how can I
do so with the arguments of the function???

Is there any way of doing it???
Why do you want to do this? Can you tell us the problem you're trying to
solve?

The point of a virtual function *not* for derived classes to be able to do
arbitrarily different things from their base class. The function should have
the same *apparent* behaviour in the base class and each of the derived
classes, even though the *implementation* may be different. If the function
changes its arguments or return type, it no longer has the same apparent
behaviour.

If you find yourself trying to do something different, you're probably
misusing virtual functions and should reconsider your design.

Philip

Aug 22 '06 #4
George schrieb:
Here is my problem:

class A {
// All share attributes
// ....
virtual void Open(void);
}
In C, you write:

void Open(void);

In C++, you usually write:

void Open();
>
class B: public A {
void Open(void);
}

class C: public A {
void Open(int i);
}

There is no problem on defining B. However with C there is. The solution
could be, setting in the parent class another virtual and define it to
NULL on B. But this is not what I like. I know that there is a
possibility of changing the return value on the children, but how can I
do so with the arguments of the function???
Describe, _what_ you really want to do, and not, _how_ you tried to get it
working.

It is hard to guess what your problem is. Maybe you want Open() and
Open(int) both working in class C, but the declaration of Open(int) hides
the inherited function.

There are two ways to solve it:
- overload Open() in class C,
- use a using-directive:

class C : public A
{
using B::Open;
void Open(int i);
};

HTH.

--
Thomas
Aug 22 '06 #5
Jens Theisen <jt***@arcor.dewrote in news:44eaea90$0$20033$9b4e6d93
@newsspool4.arcor-online.net:
George wrote:
>Is there any way of doing it???

What exactly do you want to do?
I want to force class B and C to have an open method. The only thing that
depending on the class I may need a parameter to be passed. The rest of the
attributes are the same.

Let's supose it is like opening a file and the serial port in this way:
File->name, serial port-number. In both cases you get a file descriptor
but I want my function Open to have different parameters.

Maybe it is not the correct way, but it is the one I have thought :(( I'm
quite new to C++ thinking.

TA.
Aug 22 '06 #6

"George" <ge*********@NOSPAMgmx.netskrev i meddelandet
news:Xn***********************@213.0.184.81...
Jens Theisen <jt***@arcor.dewrote in
news:44eaea90$0$20033$9b4e6d93
@newsspool4.arcor-online.net:
>George wrote:
>>Is there any way of doing it???

What exactly do you want to do?

I want to force class B and C to have an open method. The only thing
that
depending on the class I may need a parameter to be passed. The rest
of the
attributes are the same.

Let's supose it is like opening a file and the serial port in this
way:
File->name, serial port-number. In both cases you get a file
descriptor
but I want my function Open to have different parameters.

Maybe it is not the correct way, but it is the one I have thought
:(( I'm
quite new to C++ thinking.
If you want B::open and C::open to be different functions, you don't
have to have an A::open. Just put common functions in class A, and
class specific functions in the different subclasses.

In your example of a socket or a file, just must use the correct
subclass anyway. In the standard library, fstream inherits from
iostream, and adds an open function (taking a file name).
Bo Persson
Aug 22 '06 #7
Jens Theisen wrote:
Your _overriding_ a function in B and _overwriting_ a function (actually
the name) in C.
I'm not sure those are the most common definitions/terms.

B is _overriding_ the function in A whilst C is _overloading_ the
function.

Overriding is where a virtual function is implemented in a sub-class
and overloading is where there are multiple functions with the same
name.

I've not come across the term _overwiting_ except to do with disk
files. C's version of the function does _occlude_ the version in A
though.

K

Aug 22 '06 #8
In your example of a socket or a file, just must use the correct
subclass anyway. In the standard library, fstream inherits from
iostream, and adds an open function (taking a file name).
See the point. If the standard library does that, why do I should it in
another way? Probably there should be no way.

Thanks.
Aug 22 '06 #9
"George" <ge*********@NOSPAMgmx.netwrote in message
news:Xn************************@213.0.184.81...
Hi all,

I'm moving my experince from C to C++ and while so I'm facing some
problems with inheritance.

Here is my problem:

class A {
// All share attributes
// ....
virtual void Open(void);
}

class B: public A {
void Open(void);
}

class C: public A {
void Open(int i);
}

There is no problem on defining B. However with C there is. The solution
could be, setting in the parent class another virtual and define it to
NULL on B. But this is not what I like. I know that there is a
possibility of changing the return value on the children, but how can I
do so with the arguments of the function???

Is there any way of doing it???

TA.
void Open(void);
and
void Open(int i);
have two different signatures, hence are two different function calls
(overloaded).

Perhaps you want somethign like:

class A {
virtual void Open( int i = 0 );
}

class B: public A {
void Open( int i = 0 );
}

class C: public A{
void Open( int i = 0 );
}

This is similar to what you are attempting to do, but not exact.

I mean, without the same signature you might as well call your Open COpen in
C since neighter is going to match up with the Open in class A without the
same signature.
Aug 22 '06 #10
George wrote:
See the point. If the standard library does that, why do I should it in
another way? Probably there should be no way.
I still don't understand what you really mean.

You said you were coming from C, so here is your OO design in C:

typedef struct
{
void (*Open) (void);
} A_interface;

void B_open(void);
void C_open(int);

A_interface B_impl = { &B_Open };

A_interface C_impl = { &C_Open }; // type error

This is also meaningless.

Jens
Aug 22 '06 #11
When I meant C, was not OO in C... I'm getting now into OO programing and
the one I said was one doubt I come up with.

I have now realized that what I want is almost imposible ;)

The think is like. There is a class that is a door. But there are many
possible doors and ways to open it. What I was trying to say is that any
door should implement a method named Open, no matter the parameters they
are required. Lets say, normal door will use Open with a key and then a
garage door will use Open with a remote controller. But both are Doors
and should have an Open method.

From the example of fstream, I realize that this characteristic cannot be
inherited as the programming language doesn't allow it.

TA. Sorry for the newbie discussion ;)

Jens Theisen <jt***@arcor.dewrote in news:44eb51fd$0$10147$9b4e6d93
@newsspool1.arcor-online.net:
>George wrote:
>See the point. If the standard library does that, why do I should it
in
>another way? Probably there should be no way.

I still don't understand what you really mean.

You said you were coming from C, so here is your OO design in C:

typedef struct
{
void (*Open) (void);
} A_interface;

void B_open(void);
void C_open(int);

A_interface B_impl = { &B_Open };

A_interface C_impl = { &C_Open }; // type error

This is also meaningless.

Jens


--
Saludos a todos
#
# Contra el terrorismo
#
Aug 23 '06 #12
George wrote:
When I meant C, was not OO in C... I'm getting now into OO programing

Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.html>

Brian (top-posters are people too!)
Aug 23 '06 #13
Sorry ;) I was doing it to not having too much text on the post.

Thanks for the advice.

"Default User" <de***********@yahoo.comwrote in news:4l3n4tF4orvU1
@individual.net:
George wrote:
>When I meant C, was not OO in C... I'm getting now into OO programing


Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.html>

Brian (top-posters are people too!)


--
Saludos a todos
#
# Contra el terrorismo
#
Aug 26 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Dennis | last post: by
39 posts views Thread by Ele | last post: by
11 posts views Thread by santosh | last post: by
12 posts views Thread by placid | last post: by
16 posts views Thread by plmanikandan | last post: by
15 posts views Thread by Philipp | last post: by
3 posts views Thread by Rahul | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.