C ++ - method that takes a pointer but gets an int

I am wondering how I will access the function call when an integer is passed to a function that takes a pointer? In my case, hasPlayedInTeam () takes a pointer to a command, however, it got an int. This causes the Q_ASSERT to hang.

Also, is my problem also known as null pointer? My professor used this term several times in a lecture, but I'm not sure what he meant.

Person p1("Jack", 22, "UCLA");
Q_ASSERT(p1.hasPlayedInTeam(0) == false);

bool Person::hasPlayedInTeam(Team *pTeam) {
  bool temp = false;
  foreach (Team* team, teamList) {
    if (team->getName() == pTeam->getName() {
      temp = true;
  return temp;



source to share

6 answers

On your phone:



an integer literal is 0

converted to a pointer NULL

. Thus, you are not actually getting an integer; you pass an integer, the compiler can automatically apply it to a null pointer (given the definition for NULL).

I think you can fix the definition hasPlayedInTeam

by asserting that its argument is not NULL, or by returning the default when passing NULL:

bool Person::hasPlayedInTeam(Team *pTeam) {
    assert(pTeam!=NULL); //-- in this case, your program will assert and halt



bool Person::hasPlayedInTeam(Team *pTeam) {
    if (pTeam == NULL)
         return false; //-- in this case, your program will not assert and continue with a sensible (it actually depends on how you define "sensible") return value




Yes, it looks like your problem is a null pointer. A null pointer means that you have a pointer that doesn't actually point to anything:

Team* team = NULL;


It just so happens that C ++ NULL

has a macro for integer 0. Stroustrup has a few comments on which he prefers to use in code.



The hasPlayedInTeam () function looks for an argument of type "Command" when you are passing an argument of type "integer", which is not correct ....



Yes, I think you are referring to a null pointer in this situation.

To handle the case when an int is passed, you can overload the function and make it behave the way you want it to when an int is passed.



In C ++ there is NULL

one that is defined as 0

(in some standard header file, cstddef

I think), so yes, the integer you pass is a null pointer. 0

- the only (as far as I know) integer that will be automatically (implicitly) converted to a pointer of any type.

In practice, I think most people prefer to use NULL

instead 0

for a null pointer.

I'm not sure why it is hanging, but dereferencing the pointer NULL

(in your expression pTeam->getName()

) should cause the program to crash if you pass it NULL

in and not just hang.



Unfortunately, the null pointer literal is one of the confusing parts of the language. Let me try:

  • For any type, there is a concept "pointer to this type". For example, you can have integers and a pointer to integers ( int x; int *y;

    ), double and point to double ( double x; double *y;

    ), Person and a pointer to Person ( Person x,*y;

    ). If X

    is a type, then "pointer to X" is the type itself , and therefore you can even find pointers to pointers to integers ( int **x;

    ) or pointers to pointers to pointers to characters ( char ***x;


  • There is a null pointer

    value for any type of pointer . This is a value that doesn't actually point to an object, so it needs to try to dereference it ("dereferencing" a pointer means reading or writing the object being pointed to). Note that the C ++ language does not guarantee that you will receive a message or crash when using a null pointer to try to reach a non-existent pointed object, but simply that you do not do it in the program, because the consequences are unpredictable. The language simply assumes that you are not going to make that mistake.

  • How is a null pointer displayed in a program? Here comes the tricky part. For reasons beyond comprehension, the C ++ language uses a strange rule: if you get any constant integer expression with a null value , then it can (if necessary) be considered a null pointer for any type.

The last rule is extremely strange and illogical and, for example, means that

char *x = 0;     // x is a pointer to char with the null pointer value (ok)

char *y = (1-1); // exactly the same (what??)

char *z = !!    !!  !!    !!  !!       !!  
          !!!   !!  !!    !!  !!       !!
          !!!!  !!  !!    !!  !!       !!
          !! !! !!  !!    !!  !!       !!
          !!  !!!!  !!    !!  !!       !!
          !!   !!!  !!    !!  !!       !!
          !!    !!   !!!!!!   !!!!!!!  !!!!!!1; // Same again (!)


and this is true for any type of pointer.

Why is it a standard indication that any expression, not just a null literal, can be considered a null pointer value? I really don't know.

Apparently, Stroustrup also found it amusing, not disgusting, as it should be (the last example with the text "NULL", written with an odd number of negatives, is in the book "The C ++ Programming Language").

Also note that there is a symbol NULL

defined in the standard headers that provide a valid definition for a null pointer value for any type. There might be a valid definition in "C" (void *)0

, but it is not valid in C ++ because void pointers cannot be implicitly converted to other pointer types, as in "C".

Note also that you can find the term in the literature NUL

(only one L), but it is an ASCII character with code 0 (represented in C / C ++ c '\0'

) and is logically distinct from a pointer or an integer.

Unfortunately, in C ++, characters are also integers, and so, for example, '\0'

is a valid null pointer value, and the same applies to ('A'-'A')

(they are integer constant expressions that evaluate to zero).

C ++ 11 increases the complexity of these already dubious rules with std::nullptr_t

and nullptr

. I cannot explain these rules because I didn’t understand them myself (and I’m not sure if I want to understand them yet).



All Articles