Static Member Function Working Like A Pointer To Data Members

N

Nosophorus

Hi!

I was reading about static member functions when I met the following
interesting piece of code:

#include <iostream>
using namespace std;

class Egg {
private:
static Egg e;
int i;
Egg(int ii) : i(ii) {}
Egg(const Egg&); // Prevent copy-construction
public:
static Egg* instance() { return &e; } //<--- THIS IS WHAT INTERESTS
ME
int val() const { return i; }
};

Egg Egg::e(47);

int main() {
cout << Egg::instance()->val() << endl; //<--- THIS IS TOO
}

It's interesting, because the function "instance()", as it returns an
Egg pointer/address, is pointing to (in main) the member function "val
()" -- which returns an int. It seems to me the "instance()" member
function works like a pointer of type Egg. Am I right?

I would appreciate any further clarifications/explanations.

Thank You!

Marcelo de Brito
 
A

acehreli

class Egg {
private:
static Egg e; [...]
static Egg* instance() { return &e; } //<--- THIS IS WHAT INTERESTS
ME

The instance member function returns the address of the static member
e.
cout << Egg::instance()->val() << endl; //<--- THIS IS TOO
It's interesting, because the function "instance()", as it returns an
Egg pointer/address,
Correct.

is pointing to (in main) the member function "val
()" -- which returns an int.

That's not correct. The Egg* that instance() returns "is pointing to
e". It is not pointing to a member function.
It seems to me the "instance()" member
function works like a pointer of type Egg. Am I right?

You are right on that one.
I would appreciate any further clarifications/explanations.

Would this make it clearer:

Egg * p = Egg::instance();
p->val();

That's the equivalent of what you have above:

Egg::instance()->val()

Maybe you confuse -> operator as "pointing to"? Possibly because it
looks like an arrow? If so, no, -> operator "dereferences" the pointer
on it's left hand side.

So, when you say

p->val();

you mean "call the val() member of what p points to." Or more simply:

SomeType p = /* ... */
p->i = 42;

The last statement says "assign 42 to the 'i' member of the object
that 'p' points to."

Ali
 
N

Nosophorus

Hi!

Thank you very much, Ali and Paavo! :)

Ali, you are right. I should have expressed me more clearly concerning
C++ dereferencing. :)

Let me ask you an extra question.

From what Ali wrote above:

Here "p" is pointing to the address returned by "instance()" (you
assign the address returned by "instance()" to "p"):

Egg * p = Egg::instance();

And now you dereference/access it:

p->val();

Correct? :)

Thank You!

Marcelo de Brito
 
N

Nosophorus

Hi!

Thank you, Paavo! :)

Now I have a more clarifying conception of dereferencing in C++!

Best Wishes!

Marcelo de Brito
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top