Our website uses cookies to enhance your browsing experience.
Accept
to the top
close form

Fill out the form in 2 simple steps below:

Your contact information:

Step 1
Congratulations! This is your promo code!

Desired license type:

Step 2
Team license
Enterprise license
** By clicking this button you agree to our Privacy Policy statement
close form
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
* By clicking this button you agree to our Privacy Policy statement

close form
Free PVS‑Studio license for Microsoft MVP specialists
* By clicking this button you agree to our Privacy Policy statement

close form
To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

close form
I am interested to try it on the platforms:
* By clicking this button you agree to our Privacy Policy statement

close form
check circle
Message submitted.

Your message has been sent. We will email you at


If you haven't received our response, please do the following:
check your Spam/Junk folder and click the "Not Spam" button for our message.
This way, you won't miss messages from our team in the future.

>
>
>
Peculiarities of virtual functions

Peculiarities of virtual functions

Jan 11 2010
Author:

I decided to describe one thing related to virtual functions because I am afraid I can forget it and return to this question once again later :).

Viva64 analyzer provides diagnosis of errors occurring in 64-bit code when a virtual function's argument changes its type. It is described in detail in the documentation on the product here: V301.

Here is an example when the analyzer generates these warnings:

class A
{
public:
  virtual int x(unsigned) { return 1; }
  virtual int y(__int64) { return 2; }
};
class B : public A
{
public:
  int x(size_t) { return 3; } //V301
  int y(ptrdiff_t) { return 4; } //V301
};
void Use()
{
  A *p = new B;
  cout << p->x(1) << " " << p->y(1) << endl;
  delete p;
}

In 32-bit mode, "3 2″ is printed while in 64-bit mode it is "1 4″. The errors in this code are successfully diagnosed by Viva64 analyzer. But an idea has stricken me recently that one should diagnose not only the changing arguments of virtual functions but the returned argument as well. I invented an example to be diagnosed as incorrect:

class A
{
public:
  virtual int x() {};
};
class B : public A
{
public:
  ptrdiff_t x() {};
};

Fortunately, this example simply will not compile in 64-bit mode and therefore no error related to a change of code behavior will appear. Visual C++ compiler generates an error message:

error C2555: 'B::x': overriding virtual function return type differs and is not covariant from 'A::x' : see declaration of 'A::x'

After this experiment I came to recalling that I seemed to have undertaken such an investigation before. So, there is no need in diagnosis of returned values. On thinking it over I decided to make a post in the blog in order not to come to this question for the third time a year later :)

Let us consider the last thing related to diagnosis of functions where both the argument and the returned type are different:

class A
{
public:
  virtual int x(int) { return 1; }
};
class B : public A
{
public:
  ptrdiff_t x(ptrdiff_t) { return 2; } //V301
};

This code does compile and has an error. Viva64 analyzer correctly makes it out and warns that the argument changes its type in a 64-bit system. After fixing this error the compiler refuses to compile the code and thus we can correct the second error - the returned argument's type.

Popular related articles


Comments (0)

Next comments next comments
close comment form