| 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
.
|
|
|
|

|
Related Articles |
|
|