This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

default parameters, virtual functions


I couldn't access bugzilla, because the password reset page I was emailed was giving errors.
I'm not sure if this is a bug (don't have the standard), but it's short:

  When a virtual function is called by another member function of the base class, it uses the base class's default parameters, while calling the derived class's actual function. On the other hand, it behaves as expected (i.e., the derived class's default parameters are used), when accessed externally.

Version info:
Reading specs from /usr/local/lib/gcc-lib/i386-pc-solaris2.8/3.2.2/specs
Configured with: ../configure --disable-nls --with-ld=/usr/ccs/bin/ld --with-as=/usr/ccs/bin/as
Thread model: posix
gcc version 3.2.2

example code (also attached):

// virtual_default_parm_test.cpp

using namespace std;

#include <iostream>

class base
{  public:
	virtual void read_out(int = 1, int = 2);
	void call_read_out();
};

class derived : public base
{  public:
	virtual void read_out(int = 0, int = -1);
};

void base::read_out(int c1, int c2)
{	cout << "This is base::read_out" << endl;
	cout << "c1 default: " << c1 << endl;
	cout << "c2 default: " << c2 << endl;
}

void base::call_read_out()
{	read_out();
}

void derived::read_out(int c1, int c2)
{	cout << "This is derived::read_out" << endl;
	cout << "c1 default: " << c1 << endl;
	cout << "c2 default: " << c2 << endl;
}

int main()
{	base b;
	derived d;

	b.read_out();
	d.read_out();
	b.call_read_out();
	d.call_read_out();
}


Program output:

This is base::read_out
c1 default: 1
c2 default: 2
This is derived::read_out
c1 default: 0
c2 default: -1
This is base::read_out
c1 default: 1
c2 default: 2
This is derived::read_out
c1 default: 1
c2 default: 2


Thanks,
Nathan

Attachment: virtual_default_parm_test.cpp
Description: Binary data


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]