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
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