Jump to content
  • Advertisement
Sign in to follow this  
mrmrcoleman

C++ : Default parameters in a pure virtual method

This topic is 4387 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

My compiler is complaining about me trying to define a default parameter for a pure virtual method. Is this normal or am I missing something? Kind regards, Mark

Share this post


Link to post
Share on other sites
Advertisement
The C++ standard only says this:

Quote:
A virtual function call uses the default arguments in the declaration of the virtual function determined by the static type of the pointer or reference denoting the object. An overriding function in a derived class does not acquire default arguments from the function it overrides.


Do you have more information? What compiler? What version? What code?

Share this post


Link to post
Share on other sites
I don't know of any reason the compiler would complain about default parameters in a pure virtul function. Can you give an example of the problem?

Skizz

Share this post


Link to post
Share on other sites
Fruny,

Apologies, I made a mistake lower in the code, my mistake. It is interesting to learn that derived methods do not inherit the default behaviour though!

Kind regards,

Mark

Share this post


Link to post
Share on other sites
Quote:
Original post by mrmrcoleman
Apologies, I made a mistake lower in the code, my mistake. It is interesting to learn that derived methods do not inherit the default behaviour though!


It makes sense, though, since at the point of call the compiler knows only the static type of the object, not its dynamic type. In other words.

class PureVirtualBase
{
public:
virtual createWindow(int width = 640, int height = 480) = 0;
};

PureVirtualBase* pObject = factoryFunction("DirectX9");
pObject->createWindow();

will work just like you expect it to, despite the fact that the concrete class of the object returned by the factory function has different default values for the parameters to createWindow(), because at compile time at the calling point there's no way to know what that concrete class is.

Share this post


Link to post
Share on other sites
If you want to achieve this behavior, you can go about it a different way. For example:

instead of:

class Base
{
public:
virtual void myMethod(int x = 3, int y = 4) = 0;
};

class Derived : public Base
{
public:
virtual void myMethod(int x, int y); // didn't keep defaults
};

You could do:

// Base.hpp:

class Base
{
public:
virtual void myMethod(int x, int y) = 0;
virtual void myMethod(int x);
virtual void myMethod();
};

// Base.cpp:

void Base::myMethod(int x)
{
myMethod(x, 4);
}

void Base::myMethod()
{
myMethod(3, 4);
}

class Derived : public Base
{
public:
virtual void myMethod(int x, int y); // didn't keep defaults
virtual void myMethod();
};

// Derived.cpp:

void Derived::myMethod(int x, int y)
{
...
}

void Derived::myMethod()
{
this->myMethod(9,10);
}

// main.cpp:

Derived derived;
Base& base = derived;

base.myMethod(); // x = 9, y = 10, as desired


That will enforce the default parameter behavior you desire and also allow you to override it. Just a thought.

P.S.: You should probably make constants instead of "magic" numbers.

[Edited by - Rydinare on October 18, 2006 9:51:42 AM]

Share this post


Link to post
Share on other sites
I personnaly find that mixing default parameters and virtual-ness is dangerous:

struct Base
{
virtual void Output(int x = 10) { std::cout << "In BASE class:" << x; }
};

struct Derived : public Base
{
virtual void Output(int x = 5) { std::cout << "In DERIVED class:" << x; }
}

void main()
{
Base* pBase = new Derived;
pBase->Output();
}


I let you discover what the output will be (provided I didnt make trivial syntax error of course). IMHO this is a bit confusing and error prone.

Share this post


Link to post
Share on other sites
Quote:
Original post by janta
I let you discover what the output will be (provided I didnt make trivial syntax error of course). IMHO this is a bit confusing and error prone.


And is exactly covered by the exerpt of the standard I quoted. [smile]

Even though *pBase really is a Derived object and thus Derived::Output gets called, pBase is a Base* and thus the default arguments from Base will be used.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!