Required mapping member initialization of type MappingQ

19 views
Skip to first unread message

Konrad Schneider

unread,
Mar 8, 2023, 8:34:47 AM3/8/23
to deal.II User Group
Dear all,
I am fairly new to deal.ii and have a question concerning the MappingQ-class of deal.ii
Why do I have to initialize the private mapping member of type MappingQ<dim> to get a compilable code? To illustrated what I mean, I did thin out the step-11 tutorial in the following way:

 
#include <deal.II/grid/tria.h>
#include <deal.II/dofs/dof_handler.h>
#include <deal.II/dofs/dof_tools.h>
#include <deal.II/fe/fe_q.h>
#include <deal.II/fe/mapping_q.h>
#include <deal.II/numerics/vector_tools.h>
using namespace dealii;

template <int dim>
class LaplaceProblem{
public:
LaplaceProblem(const unsigned int mapping_degree,
const unsigned int fe_degree=1);
void run();
private:
Triangulation<dim> triangulation;
const unsigned int fe_degree;
FE_Q<dim> fe;
DoFHandler<dim> dof_handler;
MappingQ<dim> mapping;
unsigned int test;
};

template <int dim>
LaplaceProblem<dim>::LaplaceProblem(const unsigned int mapping_degree,
const unsigned int fe_degree)
: fe_degree(fe_degree)
, fe(fe_degree)
, dof_handler(triangulation)
// , mapping(mapping_degree)
{
}

template <int dim>
void LaplaceProblem<dim>::run(){
}
int main(){
const unsigned int mapping_degree=2;
LaplaceProblem<2>(mapping_degree).run();
}


Only if I uncomment line 30 of my code (// , mapping(mapping_degree)) the program compiles. Why is that? In my understanding, the program should compile just fine without the initialization of the mapping member variable since there is no requirement of initializing member variables upon constructing an object, is would be the case for my member variable unsigned int test  .
I would be grateful for any explanation.
Many thanks in advance.

Wolfgang Bangerth

unread,
Mar 8, 2023, 9:03:13 AM3/8/23
to dea...@googlegroups.com
On 3/8/23 06:34, Konrad Schneider wrote:
>
> Only if I uncomment line 30 of my code (// , mapping(mapping_degree)) the
> program compiles. Why is that? In my understanding, the program should compile
> just fine without the initialization of the mapping member variable since
> there is no requirement of initializing member variables upon constructing an
> object, is would be the case for my member variable unsigned int test.

This isn't true. In a constructor, *all* member variables [1] are initialized
by calling their constructors. If you explicitly list a member in the
initializer list (after the ':'), then the constructor arguments so specified
are taken. If you don't explicitly list a variable, then the default
constructor of the variable's class is called. But the latter only works if
the class *has* a default constructor. It turns out that MappingQ does not:
The only constructor there is requires you to provide the polynomial degree.
As a consequence, the compiler cannot do a default-initialization, and you are
forced to explicitly list the variable in the initializer list.

Best
W.


[1] This is not quite true: all *class type* member variables are initialized,
whereas variables of built-in types like int, double, etc are not unless you
explicitly initialize them.

--
------------------------------------------------------------------------
Wolfgang Bangerth email: bang...@colostate.edu
www: http://www.math.colostate.edu/~bangerth/


Konrad Schneider

unread,
Mar 8, 2023, 9:19:06 AM3/8/23
to deal.II User Group
Dear Wolfgang,

thanks for clearing this up and responding so fast. Your explanation makes sense to me.

Best
Konrad

Reply all
Reply to author
Forward
0 new messages