C++ idiocy (noob help required)

Can anyone help me with this little glitch I have.

void myClass::function1()
{
//does stuff
}

void myClass::function2()
{
// does more stuff

// now wants to call function one on this instance of the class
this.function1(); // doesn't work
myClass.funciton1(); //doesn't work
//etc etc
}

thing = new myclass;
thingy.function2();

My thingy can’t access function 1 from function 2.
Anyone know the answer to what is probably an easy question?

Thanks
Chimp

I’m also fairly new, but I think the problem is:

// now wants to call function one on this instance of the class
this.function1(); // doesn't work
myClass.funciton1(); //doesn't work
//etc etc 

For

this.function1();

all you need to put in is

function1();

I don’t know, but that’s what I did once to call the function. Also, I’m not sure, but you might need a prototype.

Your right, I had tried that initially and fouled up.
Now, of course, I see that it had nothing to do with referencing the function.
I left a variable definition outside an if statement and it was executing regardless of the result of the if statement.

Thanks for that though.

Chimp

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.

Thanks earlye. I was going to post this earlier but ran out of time. When I came back today I had to double-take to make sure I didn’t actually post something… You said exactly what I had in mind.

2 red stars for your nose.

:slight_smile:

Yeah, thanks for your helps guys and sorry again for setting off the idiot alarm.

The bug was in no way related to calling function1() but I suspected it was and promptly changed it from the correct format - funtion 1(); to eveything else under the sun compounding a dumb mistake even further.

My bad :frowning:

Chimpoid