Inheritance Question

B

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
 
A

Adios

BCC said:
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/d...vclang98/html/_pluslang_virtual_functions.asp
 
B

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
 
A

Adios

BCC said:
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 :)
 
J

Jonathan Turkanis

BCC said:
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
 
E

Efrat Regev

BCC said:
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.
 
D

davidrubin

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
 
D

David Harmon

On Fri, 28 Jan 2005 18:55:37 GMT in comp.lang.c++, "BCC"
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.
 

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,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top