Unicorn with delicious cookie
Nous utilisons des cookies pour améliorer votre expérience de navigation. En savoir plus
Accepter
to the top
>
>
>
Peculiarities of virtual functions

Peculiarities of virtual functions

11 Jan 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

S'abonner

Comments (0)

close comment form
close form

Remplissez le formulaire ci‑dessous en 2 étapes simples :

Vos coordonnées :

Étape 1
Félicitations ! Voici votre code promo !

Type de licence souhaité :

Étape 2
Team license
Enterprise licence
** En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité
close form
Demandez des tarifs
Nouvelle licence
Renouvellement de licence
--Sélectionnez la devise--
USD
EUR
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
La licence PVS‑Studio gratuit pour les spécialistes Microsoft MVP
close form
Pour obtenir la licence de votre projet open source, s’il vous plait rempliez ce formulaire
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
I want to join the test
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
check circle
Votre message a été envoyé.

Nous vous répondrons à


Si l'e-mail n'apparaît pas dans votre boîte de réception, recherchez-le dans l'un des dossiers suivants:

  • Promotion
  • Notifications
  • Spam