top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

C++: Why static function of a class is not allowed to access non-static data members ?

+1 vote
339 views
C++: Why static function of a class is not allowed to access non-static data members ?
posted Sep 20, 2018 by Ganesh Kumar

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+2 votes

Static function is not associated with class object. So it can be called just with the class name. (without creating the object instance from that class). This is also applicable to static data member.

Nonstatic variables are associated with objects. i.e when instance of object is created from the class, it will have its own copy of non static variable.

Since static function doesnot know object instance, so it cant access the non-static data member.

answer Sep 20, 2018 by Pdk
Similar Questions
0 votes

Given a Calendar class (there are three fields, year, month, day) and a number of N, Implement a function that returns the calendar after N days.

Example:
Input: {2017, 3,20} and 12,
Output: {2017,4, 1}

0 votes

While doing design for a software, what things enforce to use template classes and template functions ?
Is it consider best practices or not ?

...