Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > virtual functions in virtual base class

Reply
Thread Tools

virtual functions in virtual base class

 
 
sreelakshmi.rajula@gmail.com
Guest
Posts: n/a
 
      08-21-2006
Does it allow virtual functions in Virtual base class?
I am getting errors in the following code.Anything wrong in this code?I
want to print Eagle's age. Can anybody clarify my doubt?

class Animal
{
public:
Animal() { age = 0 ;}
virtual int GetAge() const { return age; }
private:
int age;
};
class Eagle : virtual public Animal
{
public:
int GetAge() { age = 4 ; return age; }
private:
int age ;
};
class Lion : virtual public Animal
{
public:
int GetAge() { age = 10 ; return age; }
private:
int age ;
};

class Griffin : public Eagle ,public Lion
{
};
int main()
{
Animal *animal = new Eagle();
animal->GetAge();
return 0 ;

}

 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      08-21-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Does it allow virtual functions in Virtual base class?
> I am getting errors in the following code.Anything wrong in this code?I
> want to print Eagle's age. Can anybody clarify my doubt?
> [code redacted]


What errors are you getting? What were you expecting? Read FAQ 5.8.
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8

 
Reply With Quote
 
 
 
 
amirkam1@yahoo.com
Guest
Posts: n/a
 
      08-21-2006
> virtual int GetAge() const { return age; }
This code should not give you any error, unless you remove const from
the GetAge method in most base class. As you have declared the function
to be const in base class but not in derived class, it is not
overriding.

Also the virtual inheritance does not resolve the error for ambiguous
functions. You need to override the function in the most derived
(Griffin) class and call the appropriate base version.

 
Reply With Quote
 
sreelakshmi.rajula@gmail.com
Guest
Posts: n/a
 
      08-21-2006
I am sorry. What you told is correct. Actually when I override GetAge()
function it'll give errors. But one more is how can I print Eagle's
age?
(E-Mail Removed) wrote:
> > virtual int GetAge() const { return age; }

> This code should not give you any error, unless you remove const from
> the GetAge method in most base class. As you have declared the function
> to be const in base class but not in derived class, it is not
> overriding.
>
> Also the virtual inheritance does not resolve the error for ambiguous
> functions. You need to override the function in the most derived
> (Griffin) class and call the appropriate base version.


 
Reply With Quote
 
amirkam1@yahoo.com
Guest
Posts: n/a
 
      08-22-2006

(E-Mail Removed) wrote:
> I am sorry. What you told is correct. Actually when I override GetAge()
> function it'll give errors. But one more is how can I print Eagle's
> age?


If the functions are overriden properly, then by means of virtual
mechanism the call to getAge will be resolved correctly. But in Griffin
class you will have to override the function and hardcode which version
of gateAge should be called (Eagle::getAge OR Lion::getAge()) to avoid
ambiguous error.

 
Reply With Quote
 
manish
Guest
Posts: n/a
 
      08-22-2006
use the following to print age
cout<<animal->GetAge();
in the main( )
instead of
animal->GetAge();

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      08-22-2006
manish wrote:
> use the following to print age
> cout<<animal->GetAge();
> in the main( )
> instead of
> animal->GetAge();
>

What are you replying to?

--
Ian Collins.
 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      08-28-2006
red floyd <(E-Mail Removed)> writes:

> (E-Mail Removed) wrote:
>> Does it allow virtual functions in Virtual base class?
>> I am getting errors in the following code.Anything wrong in this code?I
>> want to print Eagle's age. Can anybody clarify my doubt?
>> [code redacted]

>
> What errors are you getting? What were you expecting? Read FAQ
> 5.8. http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8
>


Why dont you just write a bot to post your "OT" responses? It would save
you a lot of time. In about 20 posts you have added help possibly once :
in all the rest you are "OT"ing and referring to netiquette
documents.

You clearly have a very high opinion of yourself.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
base class public type (non template and template base class) Hicham Mouline C++ 1 04-20-2009 03:28 PM
Access of base class' private base class: qualification required, why Alf P. Steinbach C++ 6 09-03-2005 04:03 PM
Virtual function 'BasicMidReader::~BasicMidReader()' conflicts with base class 'base 'TMemoryStream' tomek C++ 2 12-01-2003 06:31 AM
Virtual function 'BasicMidReader::~BasicMidReader()' conflicts with base class 'base 'TMemoryStream' tomek C++ 3 11-30-2003 12:18 AM
Virtual functions and virtual base classes - I'm confused Michael Winter C++ 9 09-23-2003 11:17 PM



Advertisments