Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling member functions from a constructor

Tags:

c++

oop

I know this question has a similar title to this: C++: calling member functions within constructor? but I am asking a more general question.

Is it good practice to call member functions from within a constructor? It makes reading the code easier and I prefer the encapsulation type way of doing it (ie. each block of code has a single objective).

An 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 of doing it than writing the validate code inside the constructor? Are there drawbacks to this method? For example is it more costly with the function overhead?

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

Cheers

like image 673
Simon Walker Avatar asked Jun 22 '10 09:06

Simon Walker


People also ask

Can you call a function in a class constructor?

Calling a method using this keyword from a constructorYes, as mentioned we can call all the members of a class (methods, variables, and constructors) from instance methods or, constructors.

Can private member functions can be called from constructor?

If a Constructor is Private then the object created in the main function cannot be initialized as private members of a class cannot be called by the main() function. So, Constructor cannot be Private.

Can we call member function using this pointer from constructor?

the constructor is the first function which get called. and we can access the this pointer via constructor for the first time. if we are able to get the this pointer before constructor call (may be via malloc which will not call constructor at all), we can call member function even before constructor call.

How do you call a member function?

A member function is declared in the class but defined outside the class and is called using the object of the class. A non-member function that is declared outside the class but called a normal function inside the main function.


2 Answers

I don't think there is anything inherently wrong in calling member functions from a constructor provided that they are not virtual functions.

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

In Java, any one of the private, static or final access modifiers will make the method safe to call from a constructor by preventing a virtual call to the superclass method. I don't think these techniques are available in Python.

like image 96
richj Avatar answered Sep 20 '22 15:09

richj


There is at least one associated "gotcha" 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 the operand of the typeid operator (5.2.8) or of a dynamic_cast (5.2.7). However, if these operations are performed in a ctor-initializer (or in a function called directly or indirectly from a ctor-initializer) before all the mem-initializers for base classes have 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 example]

like image 28
recentlybitten Avatar answered Sep 23 '22 15:09

recentlybitten