| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Sat Jan 28, 2006 9:00 pm Post subject: Class inheritance question |
|
|
I have 2 classes like this:
class Parent
{
public:
void divide( B* element);
protected:
virtual void divide(A* bd);
}
class Child : public Parent
{
protected:
virtual void divide(A* bd);
}
And I have code which calls this:
Child c;
c.divide(new B());
Why the above code can't compile? Why Child does not inherit 'divide(
B* element);' from Parent?
Thank you. |
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Sat Jan 28, 2006 9:00 pm Post subject: Re: Class inheritance question |
|
|
* Allerdyce.John (AT) gmail (DOT) com:
| Quote: | I have 2 classes like this:
class Parent
{
public:
void divide( B* element);
protected:
virtual void divide(A* bd);
}
class Child : public Parent
{
protected:
virtual void divide(A* bd);
}
And I have code which calls this:
Child c;
c.divide(new B());
Why the above code can't compile?
|
It could, depending on what types B and A are.
However, if B and A are unrelated types it won't compile.
That's because the declaration of divide in Child, _hides_ the inherited
divide(A*), with respect to name lookup.
If that's the problem you're facing, you can add
using Parent::divide;
in class Child.
| Quote: | Why Child does not inherit 'divide( B* element);' from Parent?
|
It does.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail? |
|
| Back to top |
|
 |
Guest
|
Posted: Sat Jan 28, 2006 11:00 pm Post subject: Re: Class inheritance question |
|
|
Yes. class A and class B are not related.
And why I don't have the same problem in Java?
Thank you. |
|
| Back to top |
|
 |
Markus Moll Guest
|
Posted: Sun Jan 29, 2006 1:00 am Post subject: Re: Class inheritance question |
|
|
Hi
Allerdyce.John (AT) gmail (DOT) com wrote:
| Quote: | And why I don't have the same problem in Java?
|
Just a wild guess... but I think that is because Java is a different
language.
Markus |
|
| Back to top |
|
 |
red floyd Guest
|
Posted: Sun Jan 29, 2006 5:00 am Post subject: Re: Class inheritance question |
|
|
Allerdyce.John (AT) gmail (DOT) com wrote:
| Quote: | I have 2 classes like this:
class Parent
{
public:
void divide( B* element);
protected:
virtual void divide(A* bd);
}
class Child : public Parent
{
protected:
virtual void divide(A* bd);
}
And I have code which calls this:
Child c;
c.divide(new B());
|
Congratulations. You have a memory leak (unless Child::divide takes
ownership of the pointer passed to it). |
|
| Back to top |
|
 |
Ben Pope Guest
|
Posted: Mon Jan 30, 2006 3:00 pm Post subject: Re: Class inheritance question |
|
|
Allerdyce.John (AT) gmail (DOT) com wrote:
| Quote: | I have 2 classes like this:
class Parent
{
public:
void divide( B* element);
protected:
virtual void divide(A* bd);
}
|
;
| Quote: | class Child : public Parent
{
protected:
virtual void divide(A* bd);
}
|
;
| Quote: | And I have code which calls this:
|
int main() {
| Quote: | Child c;
c.divide(new B());
|
}
| Quote: | Why the above code can't compile? Why Child does not inherit 'divide(
B* element);' from Parent?
|
Missing semicolons, missing definition of B, missing main etc.
Please post code that compiles, or at least as close as possible.
Ben Pope
--
I'm not just a number. To many, I'm known as a string... |
|
| Back to top |
|
 |
|