How to mock static method call by constructor?

Can you call public methods from a constructor in C++?

  • Can I call public function in constructor to initialize the private member variables? I Googled and found one should not use virtual functions and public functions in constructor, whereas we can always use private member functions in constructor.

  • Answer:

    There is no prohibition against calling public methods from a constructor in C++. Nothing wrong with it at all. There is also nothing wrong with public methods initializing private variables, or vice versa. Whether a method is public, private, or protected is irrelevant—those are just access restrictions. There are two things of which a constructor need be mindful: Calling virtual methods and calling functions that manipulate member variables that are initialized later in the constructor. The latter isn't a rule, just common sense, but is an easy mistake to make.

Robert Love at Quora Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.