Here’s why the first one doesn’t work. “this” is a pointer, not a class, namespace, object, or reference. Therefore, if you really want to call a member of “this” through the “this” pointer, you need:
...
this->function1();
Note that using “this” isn’t usually necessary, because all of its members are visible from inside a member function. So the simplest way to call another member is:
function1();
The second one doesn’t work because “myClass” is a class, not a pointer, object, or reference. To call a member of a class or a namespace, you use the scope-resolution operator, “::”. Like so:
myClass::function1();
It’s not as rare to need to do this. For example, when you need to force a call to a member of a base class:
class basic
{
public:
virtual void do_something( ) { /* AMAZING STUFF! */ }
};
class derived
: public basic
{
public:
virtual void do_something( )
{
/* PREPARE TO do_something AMAZING! */
basic::do_something( );
}
};
Here, if you simply used “do_something()” or its equivalent, “this->do_something(),” it would call derived::do_something rather than basic::do_something. That would almost assuredly lead to a crash, since in that call you would again call “do_something()”, which would in turn call “do_something(),” ad nauseum.
I hope this little table helps you understand:
Member Access Operators
type-of-identifier operator example
----------------------------------------------------------
namespace :: std::cout << "hello"
class :: a_class::member
pointer -> p_object->member
reference . ref_object.member
object . an_object.member
I’m leaving out things like “pointer-to-member-function” and “pointer-to-function”, but they’re a bit more advanced and require a whole separate discussion.