Little question about constructor

17 views
Skip to first unread message

yuesu jin

unread,
Sep 3, 2019, 10:44:24 PM9/3/19
to deal.II User Group
Hi all,

  I have a question about the constructor initialization list. In some tutorials, we initialize the dof_handler in the constructor with parameter triangulation, however, the triangulation is initialized in some member function within this class, which means when we initialize the class, we have not had the triangulation yet, I don't know why this method works?

Best regards,
Yuesu

Konrad

unread,
Sep 4, 2019, 6:03:07 AM9/4/19
to deal.II User Group
Hi Yuesu,

I think this is rather a general C++ question. Once you create an object all members are initialized by invoking a constructor. If you do not declare any constructor the compiler will silently generate a set of constructors. The default constructor that takes no arguments is one of them. This will initialize all members in the order they are given. If you have a member of a primitive type like double or int memory for an int or double will be reserved (but not initialized with a value yet). If you have a class member it also needs to be initialized somehow so the compiler will look for a default constructor.

What happens in your example is that the triangulation actually does get initialized by invoking its default constructor which initializes an empty triangulation. The dof_handler then can be called with the triangulation as an argument to store a (smart) pointer to the triangulation.

See this documentation for example:


It is often good to know what the C++ compiler (secretly) does without explicitly telling you and what functions it generates if you do not provide them explicitly (and when you must do so). I really recommend the book by Scott Meyers "Effective C++: 55 ways ..."

Hope that helps.

Best,
Konrad

yuesu jin

unread,
Sep 4, 2019, 1:41:40 PM9/4/19
to deal.II User Group
Dear Konrad,
  Appreciating for your detailed explanation, it really helps!! Thank you! I am learning c++ by the means of studying deal.ii, so actually I am a layman both in deal.ii and c++. I will read the reference page and books you recommended. Thanks again!!
Best regards,
Yuesu

--
The deal.II project is located at http://www.dealii.org/
For mailing list/forum options, see https://groups.google.com/d/forum/dealii?hl=en
---
You received this message because you are subscribed to the Google Groups "deal.II User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dealii+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dealii/fd3807d1-51e6-4daa-8eed-5da2eec1336b%40googlegroups.com.


--
Yuesu Jin,
Ph.D student,
University of Houston,
College of Natural Sciences and Mathematics,
Department of Earth and Atmospheric Sciences,
Houston, Texas 77204-5008

Reply all
Reply to author
Forward
0 new messages