pure virtual parameters

A

Alex

I have a doubt. Given a function like this:

class bar {
public:
virtual void foo(int i = 0) = 0;
};

when implementing this virtual function, will the above enforce the default
parameter in all implementations?

class der : public bar
{
void foo(int i) {}
};

or should I again specify "int i = 0"?

Thanks.
 
A

Andre Kostur

I have a doubt. Given a function like this:

class bar {
public:
virtual void foo(int i = 0) = 0;
};

when implementing this virtual function, will the above enforce the
default parameter in all implementations?

Nope. (At least not where foo() is called through a pointer/reference to a
more-derived type....)
class der : public bar
{
void foo(int i) {}
};

or should I again specify "int i = 0"?

You again specify "int i = 0".
 
B

Bo Hunter

Alex said:
I have a doubt. Given a function like this:

class bar {
public:
virtual void foo(int i = 0) = 0;
};

when implementing this virtual function, will the above enforce the
default parameter in all implementations?

class der : public bar
{
void foo(int i) {}
};

or should I again specify "int i = 0"?

Thanks.

I took your code pasted it in to a test project
and calling der.foo() will produce a error on GCC 3.2.
Then added void foo( int i = 0 ) and calling der.foo()
does not produce a error. So I guess that it does not
get the default parameter.
 
P

Phlip

Alex said:
I have a doubt. Given a function like this:

class bar {
public:
virtual void foo(int i = 0) = 0;
};

when implementing this virtual function, will the above enforce the default
parameter in all implementations?

class der : public bar
{
void foo(int i) {}
};

or should I again specify "int i = 0"?

Specify again (or pick another method to default the parameter).

Default parameters resolve at compile time, so they resolve based on their
object's static type. Virtual methods resolve at runtime.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top