I was testing out classes and I made this class
class Point
{
private:
int x,y;
public:
void setit(int new_x,int new_y);
void set_x(int new_x);
void set_y(int new_y);
int get_x();
int get_y();
};
now I went ahead and wrote the function definitions for all the public functions but,
There is something that puzzled me when i was writing the void set(int new_x,int new_y);
function definition
void Point::setit(int new_x, int new_y){
Point::set_x(new_x);
Point::set_y(new_y);
}
void Point::setit(int new_x, int new_y){
set_x(new_x);
set_y(new_y);
}
I noticed that the two previous function definitions have the exact same effect.
I thought that without the :: operator it wouldn’t work because it would search for the functions outside the class, since I no longer signify they are in the Point class
Can anyone explain why they both have the same effect??
Thank You.
::is the scope resolution operator; it can tell the compiler exactly where to look for a name.The
Point::set_xis simply an extended syntax for calling a member function.Is short for
And
Is equivalent for
It allows you to select which function to call when a class hides a function in a parent class. For instance:
One thing you can do with this syntax is call the member function of a parent class from the overridden virtual function of a base class, allowing you to use the “default implementation” provided by the base class. You can also do it from outside the class, similar to hidden functions: