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 7 1088
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
"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.
> 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
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 :-)
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
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: KK |
last post by:
Windows Forms Inheritance, Incomplete?
I was playing around with Windows Forms
and found out this Forms Inheritance feature.
The moment I saw that, I felt this can be
used effectively if the...
|
by: KK |
last post by:
** Posting it here cause after couple of days no body responded.**
I was playing around with Windows Forms
and found out this Forms Inheritance feature.
The moment I saw that, I felt this can...
|
by: Dave Theese |
last post by:
Hello all,
The example below demonstrates proper conformance to the C++ standard.
However, I'm having a hard time getting my brain around which language rules
make this proper...
The error...
|
by: __PPS__ |
last post by:
Hello everybody, today I had another quiz question
"if class X is privately derived from base class Y what is the scope of
the public, protected, private members of Y will be in class X"
By...
|
by: Matthew Louden |
last post by:
I want to know why C# doesnt support multiple inheritance? But why we can
inherit multiple interfaces instead? I know this is the rule, but I dont
understand why. Can anyone give me some concrete...
|
by: Ben Blank |
last post by:
I'm writing a family of classes which all inherit most of their methods and
code (including constructors) from a single base class. When attempting to
instance one of the derived classes using...
|
by: VR |
last post by:
Hi, I read about Master Pages in ASP.Net 2.0 and after implementing some
WinForms Visual Inheritance I tryed it with WebForms (let's say .aspx pages,
my MasterPage does not have a form tag itself...
|
by: Noah Roberts |
last post by:
Is there anything that says that if you virtually inherit from one
class you have to virtually inherit from anything you inherit from?
|
by: RSH |
last post by:
I have a simple question regarding inheritance in a web form.
I have a DropDownList in an aspx form. It is called DropDownList1
I have a class that will be overriding the render event so I...
|
by: RSH |
last post by:
Hi,
I am working on some general OOP constructs and I was wondering if I could
get some guidance.
I have an instance where I have a Base Abstract Class, and 4 Derived
classes. I now need to...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |