Why compilation error here?



 DEVELOP > c-Plus-Plus > Why compilation error here?

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Goran"
Date: 12 Sep 2006 04:45:13 AM
Object: Why compilation error here?
Hi all!
Example here doesn't compile in both VC++ and gcc. It's like Get(int&)
somehow hides Get() in CDerived. Does anybody has an idea why?
class CBase
{
public:
int Get() const { int i; Get(i); return i;};
virtual bool Get(int& i) const { return false; }
};
class CDerived : public CBase
{
int j;
public:
virtual bool Get(int& i) const { i = j; return true; }
};
void WTF()
{
CDerived d;
int test = d.Get(); // error 2660: function does not take 0 arguments
// int test = d.CBase::Get(); works, but yuck!
CBase b;
test = b.Get(); // OK.
}
Thanks,
Goran,
.

User: "Gavin Deane"

Title: Re: Why compilation error here? 12 Sep 2006 06:02:33 AM
Goran wrote:

Hi all!

Example here doesn't compile in both VC++ and gcc. It's like Get(int&)
somehow hides Get() in CDerived.

It's like Get(int&) somehow hides Get() because that's exactly what
does happen.

Does anybody has an idea why?

Why is because that's what the language rules say. This section of the
FAQ
http://www.parashift.com/c++-faq-lite/strange-inheritance.html
should show you ways round the problem.
<snip code>
Gavin Deane
.

User: "Carlos Martinez"

Title: Re: Why compilation error here? 12 Sep 2006 05:00:42 AM
Goran wrote:

Hi all!

Example here doesn't compile in both VC++ and gcc. It's like Get(int&)
somehow hides Get() in CDerived. Does anybody has an idea why?

class CBase
{
public:
int Get() const { int i; Get(i); return i;};
virtual bool Get(int& i) const { return false; }
};

class CDerived : public CBase
{
int j;
public:
virtual bool Get(int& i) const { i = j; return true; }
};

void WTF()
{
CDerived d;
int test = d.Get(); // error 2660: function does not take 0 arguments
// int test = d.CBase::Get(); works, but yuck!

CBase b;
test = b.Get(); // OK.
}


Thanks,
Goran,

I'm not sure but I think when you redefine Get in CDerived for int
parameter, you're hiding Get().
I think it's due to:
http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.9
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER