Article View: pl.comp.lang.c
Article #309705Re: Dziedziczcenie i metody
From: Bartłomiej Golenko
Date: Fri, 22 Jun 2018 14:41
Date: Fri, 22 Jun 2018 14:41
62 lines
2135 bytes
2135 bytes
radoslaw@korzeniewski.net <radoslaw@korzeniewski.net> wrote:
> Witam,
>
> Mam klasę bazową A:
> class A {
> bool send() { return send(0); };
> virtual bool send(int flags);
> }
> oraz klasę pochodną B:
> class B: public A {
> bool send(int flags);
> }
> chciałbym teraz móc skorzystać z B.send() ale kompilator wywala:
> error: no matching function for call to ‘B::send()’
> b.send();
> ^
> note: candidate: virtual bool B::send(int)
> obejście w postaci zdefiniowania B::send() jakoś mi nie pasuje. Przyjmę wszelaką krytykę i pomoc, także query do google.
Rozdziel interfejs od implementacji.
class A
{
// prywatna wirtualna implementacja
virtual bool send_impl(int flags);
public:
// publiczny interfejs
bool send() { return send_impl(0); };
};
bool A::send_impl(int flags)
{
return flags == 0;
}
class B: public A
{
bool send_impl(int flags);
};
bool B::send_impl(int flags)
{
return flags == 1;
}
int main()
{
A a;
B b;
std::cout << a.send() << std::endl;
std::cout << b.send() << std::endl;
A * ptr = &a;
std::cout << ptr->send() << std::endl;
ptr = &b;
std::cout << ptr->send() << std::endl;
}
Google(private virtual function c++)
B.
Technical Details
Message-ID:
<slrnpiq2k1.smo.bartek@kts.pwr.edu.pl>Path:
mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!newsfeed.pionier.net.pl!pwr.wroc.pl!news.wcss.wroc.pl!not-for-mailReferences:
<342f89bb-b3d4-4c87-9dd7-97a572d63ab0@googlegroups.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!newsfeed.pionier.net.pl!pwr.wroc.pl!news.wcss.wroc.pl!not-for-mail From: Bartłomiej Golenko <bartek@zts.invalid> Newsgroups: pl.comp.lang.c Subject: Re: Dziedziczcenie i metody Date: Fri, 22 Jun 2018 14:41:05 +0000 (UTC) Organization: Politechnika Wroclawska Lines: 65 Message-ID: <slrnpiq2k1.smo.bartek@kts.pwr.edu.pl> References: <342f89bb-b3d4-4c87-9dd7-97a572d63ab0@googlegroups.com> Reply-To: pisz_na_newsy@albo.wcale.pl NNTP-Posting-Host: zts.ita.pwr.wroc.pl Mime-Version: 1.0 X-Trace: z-news.wcss.wroc.pl 1529678465 23253 156.17.38.22 (22 Jun 2018 14:41:05 GMT) X-Complaints-To: abuse@news.pwr.wroc.pl NNTP-Posting-Date: Fri, 22 Jun 2018 14:41:05 +0000 (UTC) User-Agent: slrn/1.0.3 (Linux) Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit