public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
From: nathansmith_2003@yahoo.com
To: gcc-gnats@gcc.gnu.org
Subject: c++/10311: Wrong default argument used in virtual function call
Date: Fri, 04 Apr 2003 16:36:00 -0000	[thread overview]
Message-ID: <20030404163338.30497.qmail@sources.redhat.com> (raw)


>Number:         10311
>Category:       c++
>Synopsis:       Wrong default argument used in virtual function call
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Fri Apr 04 16:36:00 UTC 2003
>Closed-Date:
>Last-Modified:
>Originator:     nathansmith_2003@yahoo.com
>Release:        2.96,3.2.1
>Organization:
>Environment:
Linux
>Description:
The default argument of the pointer type's function is being used rather than the actual type when calling a virtual function with default arguments. i.e. if you have a class B derived from another class A, both with a virtual function print which accepts a char*, B::print will use A::print's default arguments when called, if the pointer to an instance of B is of type A*.

Section dcl.fct.default par 10A of the ANSI C++ spec states that "An overriding function in a derived class does not acquire default arguments from the function it overrides."

This bug is persistent even when -pedantic is specified.
>How-To-Repeat:
When executing the following code, one would expect the output to be "hi there" due to the default arguments of the virtual functions. However, the unexpected result of "hihi" is produced instead. 

#include <iostream>

class A {
public:
        virtual void print(const char *x = "hi");
};

void A::print(const char *x) { std::cout << x; }

class B : public A {
public:
        virtual void print(const char *x = " there");
};

void B::print(const char *x) { std::cout << x; }

int main(void)
{
        A *a = new A;
        A *b = new B;

        a->print();
        b->print();

        std::cout << std::endl;

        delete a;
        delete b;

        return 0;
}
>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:


             reply	other threads:[~2003-04-04 16:36 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-04-04 16:36 nathansmith_2003 [this message]
2003-04-04 17:01 nathan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20030404163338.30497.qmail@sources.redhat.com \
    --to=nathansmith_2003@yahoo.com \
    --cc=gcc-gnats@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).