C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Class inheritance question

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Sat Jan 28, 2006 9:00 pm    Post subject: Class inheritance question Reply with 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? Why Child does not inherit 'divide(
B* element);' from Parent?

Thank you.
Back to top
Alf P. Steinbach
Guest





PostPosted: Sat Jan 28, 2006 9:00 pm    Post subject: Re: Class inheritance question Reply with quote



* 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






PostPosted: Sat Jan 28, 2006 11:00 pm    Post subject: Re: Class inheritance question Reply with quote



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





PostPosted: Sun Jan 29, 2006 1:00 am    Post subject: Re: Class inheritance question Reply with quote

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





PostPosted: Sun Jan 29, 2006 5:00 am    Post subject: Re: Class inheritance question Reply with quote

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





PostPosted: Mon Jan 30, 2006 3:00 pm    Post subject: Re: Class inheritance question Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.