By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,847 Members | 2,206 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,847 IT Pros & Developers. It's quick & easy.

Inheritance Question

P: n/a
BCC
If I have a base class like this:

class A {
public:
void Foo() { Foo1(); }
void Foo1() { Foo2(); }
void Foo2() { int x = 0; }
};

And I derive from it and override Foo2():
class B: public A {
public:
void Foo2() { int y = 1; }
}

Now if I call Foo() from an instance of my derived class:
B b;
b.Foo();

When it gets to Foo2() it calls the Foo2() from my base class and not my
derived class.

Is this normal behavior? So if I want to override one small function buried
in several other function calls I have to explicitly override all those
functions in my derived class?

Thanks,
Bryan
Jul 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
BCC wrote:
If I have a base class like this:

class A {
public:
void Foo() { Foo1(); }
void Foo1() { Foo2(); }
void Foo2() { int x = 0; }
};

And I derive from it and override Foo2():
class B: public A {
public:
void Foo2() { int y = 1; }
}

Now if I call Foo() from an instance of my derived class:
B b;
b.Foo();

When it gets to Foo2() it calls the Foo2() from my base class and not my
derived class.

Is this normal behavior? So if I want to override one small function buried
in several other function calls I have to explicitly override all those
functions in my derived class?


Make foo2 in A a virtual.

class A
{
public:
void Foo() { Foo1(); }
void Foo1() { Foo2(); }
virtual void Foo2() { int x = 1; }
};
class B: public A
{
public:
void Foo2() { int y = 1; }
};

http://msdn.microsoft.com/library/de..._functions.asp
Jul 23 '05 #2

P: n/a
"BCC" <bc*@akanta.com> wrote in message
news:OB******************@newssvr13.news.prodigy.c om...
If I have a base class like this:

class A {
public:
void Foo() { Foo1(); }
void Foo1() { Foo2(); }
void Foo2() { int x = 0; }
};

And I derive from it and override Foo2():
class B: public A {
public:
void Foo2() { int y = 1; }
}

Now if I call Foo() from an instance of my derived class:
B b;
b.Foo();

When it gets to Foo2() it calls the Foo2() from my base class and not my
derived class.
Which is what it should do.

Is this normal behavior?
Yes. This is very logical: just think about the this pointer. When you
call Foo in B, it calls Foo in A, and then Foo1 in A. Now the
this pointer is to an A object. if you'd call p->Foo2(), and p is a pointer
to an A object, it wouldn't call Foo2 of a B object. In this case, just
think of it as
this->Foo2()

So if I want to override one small function buried in several other function calls I have to explicitly override all those
functions in my derived class?


Not sure what you mean. If you'd declare Foo2 in A as

virtual void Foo2() { int x = 0; }

then your derived Foo2 would be called.


Jul 23 '05 #3

P: n/a
BCC
> Not sure what you mean. If you'd declare Foo2 in A as

virtual void Foo2() { int x = 0; }

then your derived Foo2 would be called.


I know. Problem is (and I should have mentioned this before) this is not my
code, and I have instructions not to change anything in the base classes.

So I cant go in and make it virtual :(.

Bryan
Jul 23 '05 #4

P: n/a
BCC wrote:
Not sure what you mean. If you'd declare Foo2 in A as

virtual void Foo2() { int x = 0; }

then your derived Foo2 would be called.


I know. Problem is (and I should have mentioned this before) this is not my
code, and I have instructions not to change anything in the base classes.

So I cant go in and make it virtual :(.


Write a new base class, say C, copy everything from A and make foo2
virtual, derive B from C :-)
Jul 23 '05 #5

P: n/a
BCC wrote:
Not sure what you mean. If you'd declare Foo2 in A as

virtual void Foo2() { int x = 0; }

then your derived Foo2 would be called.


I know. Problem is (and I should have mentioned this before) this is
not my code, and I have instructions not to change anything in the
base classes.

So I cant go in and make it virtual :(.


You might want to look at item 37 in Scott Meyer's More Effective C++: "Never
redefine an inherited nonvirtual function."

Jonathan
Jul 23 '05 #6

P: n/a
You want your new class B to contain A, not derive from it. Then, B
can delegate to A as necessary through its public interface. /david

Jul 23 '05 #7

P: n/a
On Fri, 28 Jan 2005 18:55:37 GMT in comp.lang.c++, "BCC"
<bc*@akanta.com> wrote,
I know. Problem is (and I should have mentioned this before) this is not my
code, and I have instructions not to change anything in the base classes.


Old proverb:
You cannot make a silk purse out of a sow's ear.

If the base class code does the WRONG thing, and you cannot change it,
then you must not use it.

Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.