public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
* c++/10311: Wrong default argument used in virtual function call
@ 2003-04-04 16:36 nathansmith_2003
  0 siblings, 0 replies; 2+ messages in thread
From: nathansmith_2003 @ 2003-04-04 16:36 UTC (permalink / raw)
  To: gcc-gnats


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


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: c++/10311: Wrong default argument used in virtual function call
@ 2003-04-04 17:01 nathan
  0 siblings, 0 replies; 2+ messages in thread
From: nathan @ 2003-04-04 17:01 UTC (permalink / raw)
  To: gcc-bugs, gcc-prs, nathansmith_2003, nobody

Synopsis: Wrong default argument used in virtual function call

State-Changed-From-To: open->closed
State-Changed-By: nathan
State-Changed-When: Fri Apr  4 17:01:25 2003
State-Changed-Why:
    not a bug. what cdl.fct.default means is that a declaration
    of B::print (char const *); would not somehow inherit
    a::print's default argment list.
    
    default arguments are attached at the call site, so will be those for A

http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=10311


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2003-04-04 17:01 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-04-04 16:36 c++/10311: Wrong default argument used in virtual function call nathansmith_2003
2003-04-04 17:01 nathan

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