Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To init or to construct

I'm reviewing some code and I'm seeing a lot of this:

class Foo
{
public:
  Foo()
  {
    // 'nuffin
  }

  void init()
  {
    // actual construction code
  }
} ;

The only advantage I can see is if you create a Foo without using a pointer and you want to hold off its construction code until later, then you can.

Is this a good idea or a bad idea?

like image 237
bobobobo Avatar asked Oct 05 '09 16:10

bobobobo


People also ask

Is constructor the same as init?

"__init__" is a reserved method in python classes. It is known as a constructor in OOP concepts. This method called when an object is created from the class and it allows the class to initialize the attributes of a class.

Is __ init __ constructor in Python?

The ConstructorThe special method __init__ is the Python constructor. With an understanding of object oriented programming and classes, let's now look at how the __init__ method works within a Python program.

Is the __ init __ method technically a constructor in python true or false and why or why not?

__init__() is not a constructor. We saw the self as the first parameter which is nothing but the object itself i.e object already exists. __init__() is called immediately after the object is created and is used to initialize it.

How do you initialize a constructor in Python?

Creating the constructor in python In Python, the method the __init__() simulates the constructor of the class. This method is called when the class is instantiated. It accepts the self-keyword as a first argument which allows accessing the attributes or method of the class.


1 Answers

I dislike it. It seems to me that after construction, an object should be... well... constructed. That code leaves it in an invalid state instead, which is almost1 never a good thing.

 

1Weasel word inserted to account for unforeseen circumstances.

like image 90
Michael Myers Avatar answered Sep 29 '22 01:09

Michael Myers