Calling member functions from the constructor

I know this question has a similar name: C ++: calling member functions in the constructor? but I ask a more general question.

Is it good to use member functions from the constructor? This makes it easier to read the code, and I prefer its type of encapsulation (i.e., each block of code has one purpose).

Illustrative example in python:

class TestClass: def __init__(self): self.validate() def validate(self): # this validates some data stored in the class 

Is this a better way to do this than write validate code inside the constructor? Are there any disadvantages of this method? For example, is this a more expensive process with overhead features?

I personally prefer it for readability, but that's just my preference.

Greetings

+6
oop
source share
5 answers

I do not think that there is anything wrong with calling member functions from the constructor if they are not virtual functions .

The problem with calling virtual member functions from the constructor is that a subclass can override a function. This will force the constructor to call the overridden implementation in the subclass before the constructor is called to subclass the part of the object.

In Java, any of the private , static, or final access modifiers will make the method safe to call from the constructor by preventing the superclass method from invoking the virtual method. I do not think these methods are available in Python.

+6
source share

There is at least one associated β€œgotcha” that you should be aware of:

N3797 12.6.2 / 14

Member functions (including virtual member functions, 10.3) can be called for an object under construction. Similarly, an object under construction can be an operand of the typeid operator (5.2.8) or dynamic_cast (5.2.7). However, if these operations are performed in the ctor initializer (or in a function called directly or indirectly from the ctor initializer) before all mem initializers for the base classes are completed, the result of the operation is undefined. [Example:

 class A { public: A(int); }; class B : public A { int j; public: int f(); B() : A(f()), // undefined: calls member function // but base A not yet initialized j(f()) { } // well-defined: bases are all initialized }; class C { public: C(int); }; class D : public B, C { int i; public: D() : C(f()), // undefined: calls member function // but base C not yet initialized i(f()) { } // well-defined: bases are all initialized }; 

- end of example]

+3
source share

I am more familiar with C ++ than with Python, but I see no problems with calling member functions from constructors, especially when this practice is capable of outputting similar code from several constructors. Anything that reduces redundancy is good in my books.

+1
source share

The main problem is that a member function must work with an object that can only be partially initialized. And if it (even by accident) passes a link to an object somewhere else, the different code should be the same. This can become quite confusing and error prone, especially after you start redefining such a function in a subclass.

In general, this practice should be avoided, or at least limited to functions that cannot be overestimated, and they should never pass a reference to an object created for any other code.

+1
source share

In terms of readability, this is definitely better. One thing you might need to ask yourself here, but is there a way to verify that the launch is correct after the object is initialized. If this is not the case, you can: a) use some kind of private initialized variable or b) use the Builder template to get your objects into a valid state before using them.

Make sure the function is closed. You do not want to interfere with the subclass by overriding it (if it is not required by design, in which case make it abstract / virtual).

0
source share

All Articles