Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Overriding Virtual Functions? Use C++0x Attributes to Avoid Bugs.

Overriding a virtual member function in C++ can go wrong in many ways, most of which the compiler won't detect. Learn how three new C++0x attributes can help you avoid the bugs associated with overriding virtual functions.

n July 2009, the C++ standards committee voted into the C++0x Working Paper new attribute tokens that regulate the overriding of virtual functions in a derived class. These new attributes ensure that an overriding function matches its base class counterpart by signature and name.

As trivial as it may seem, overriding a virtual member function can go wrong in many ways, most of which go undetected by the compiler. This 10-Minute Solution shows how to use the new C++0x attributes to avoid a few common bugs associated with overriding virtual functions.

While overriding a virtual function, you accidentally misspell the function's name or inadvertently changed its parameter list. The compiler doesn't catch these bugs.

Add the [[base_check]], [[hiding]], and [[override]] attributes to your derived classes. This will help the compiler catch errors that are otherwise undetectable.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date