Why is it that I have to use :: for some methods/declarations but not others?

84 Views Asked by At

I am trying to wrap my head around the way C++ uses the scope resolution operator.

In this code snippet, I have a string pointer gridPtr, which needs the specification that it comes from the standard library. I sort of understand this, because the <string> header is in the standard library. However, printf() doesn't need that same std:: in front of it, despite being from the <cstdio> header, which is also in the standard library.

Why is this?

#include "grid.h"

Grid::Grid(int a, int b){
    std::string* gridPtr; 
    gridPtr = new std::string(a,b);
}

Grid::Grid(int a, int b, std::string color){
    std::string* gridPtr;
    gridPtr = new std::string(a,b);
    printf("Your color is %s",color);
}

I also tried adding std:: to printf() and there were no errors, adding further to my confusion.

0

There are 0 best solutions below