Say I have a class and I have a static member in it, but I don’t create any objects of that type. Will the memory be occupied for the static variable? If it would be occupied, what is the point of putting it in a class?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No.
Static members don’t belong to the instances of class. They don’t increase instances and class size even by 1 bit!
Output:
That is, size of
AandBis exactly the same. Static members are more like global objects accessed throughA::j.See demonstration at ideone : http://www.ideone.com/YeYxe
$9.4.2/1 from the C++ Standard (2003),
$9.4.2/3 and 7 from the Standard,
As I said, static members are more like global objects!