Tim
Tim

Reputation: 90

does c++ create an instance when declare a static member?

does c++ create an instance when declare a static member?
I think this is a concept question, maybe. I am not so sure.
I mean when I declare a class with a static member inside, would there be a real space for the static member? As I knew, a static member could only exit uniquely once in a class. Is the static space and function there after I declared it? If so, does it mean I can call a function (static?) without defining a real object? Thanks

Upvotes: 0

Views: 105

Answers (2)

Prinz Km
Prinz Km

Reputation: 323

Actually instance of class is never created automatically.but even before creating an instance you can call that variable...static members are seperately treated common to all instances of a class.

Upvotes: 0

Carl Norum
Carl Norum

Reputation: 224864

  1. Yes, if you declare a static member, real space for it exists. It's basically like a global variable in that sense, except for the limited scope within which it's accessible.

  2. Yes, you can call a static function without making an instance.

Upvotes: 2

Related Questions