Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static method or non-static method using static field

Tags:

c++

c++11

I have a static const std::vector<SomeType> field in my class. I have two private methods that uses this field (searching methods). They do not use another members of my class. Should they be define as static or normal methods?

What should I prefer? What are advantages/disadvantages?

like image 985
peter55555 Avatar asked Dec 14 '25 02:12

peter55555


1 Answers

Methods that reference only static members should generally be defined static. This lets you access these methods without creating an instance of a class, and also access them from static context (e.g. from a non-member function).

The only exception to this rule is when you need to make this access from a member function defined virtual. In this case you have no option to make your function static, because static functions cannot override virtual functions.

like image 95
Sergey Kalinichenko Avatar answered Dec 15 '25 18:12

Sergey Kalinichenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!