static member question

J

jasonkong1989

Question:

The below code compiles, but i am not sure how. I have a static member
variable that, when defined, calls a private constructor. I was under
the impression that the private constructor would be inaccessable to
the static member variable. Where am I mistaken?

class MyClass
{

public:
static MyClass m_myclass;
static int myfunc(){return 5;}

private:
MyClass(int x=9){int y = x; y++;} //private default constructor

};

MyClass MyClass::m_myclass(9); //m_myclass is defined, private
constructor is called

int main( )
{
int x = MyClass::m_myclass.myfunc(); //MyClass::m_myclass is
actually used
x++;
return 0;

}
 
E

Erik Wikström

Question:

The below code compiles, but i am not sure how. I have a static member
variable that, when defined, calls a private constructor. I was under
the impression that the private constructor would be inaccessable to
the static member variable. Where am I mistaken?

Static members are members, so they have access to private parts of the
class.
int x = MyClass::m_myclass.myfunc();

I would normally use MyClass::myfunc() just since the function is static.
 

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,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top