What does this C++ syntax means? [duplicate] - c++

This question already has answers here:
C++, What does the colon after a constructor mean? [duplicate]
(6 answers)
Closed 2 years ago.
I am trying to understand the inner workings of a cpp code.
The bit I am focusing is composed of 2 .cpp / .h.
The Base class is called Ionization.
The Derived class is called IonizationTunnel.
Inside IonizationTunnel.cpp I see the following:
#include "IonizationTunnel.h"
IonizationTunnel::IonizationTunnel( Params &params, Species *species ) : Ionization( params, species ) {
code
code which uses params and species
code
}
I understand that it is about IonizationTunnel constructor IonizationTunnel(Params &params, Species *species) which needs to be written with its ''prefix'' IonizationTunnel:: so that it is clear it is coming from IonizationTunnel class.
I do not understand the : Ionization() end-bit. Is it a call to the Base class constructor? If so, why is it there and why isn't it written as :Ionization{params, species}?
Thank you!

Yes, it's called a member initializer list. It's the only place you can call the base constructor, there is no super() like in Java or Python.
Both () and {} are ok for initialization: the former performs "direct initialization", the latter performs "direct list initialization".
Their behavior can differ sometimes. E.g. vector<int> v(1, 2) creates a vector of a single element (2), while vector<int> v{1, 2} creates a vector of two elements.
() is more common for constructing base classes as it was there long before C++11. Its behavior is also less ambiguous before C++20: it can only call a base constructor.

Related

Object creation with parameters not possible in c++? [duplicate]

This question already has answers here:
Why can't member initializers use parentheses?
(2 answers)
Initialization of member variable via parentheses doesn't work [duplicate]
(1 answer)
can not define and init a class member by Parentheses [duplicate]
(1 answer)
Closed 9 months ago.
I'm trying to neatly create a objects within a class and I've run into what seems to me to be an odd limitation in c++, and I wondered if I'm just missing a syntax trick or whether it's truly impossible; specifically it seems like I cannot explicitly instantiate a class object inside another class if the constructor of the former has parameters (the parameters are constant) generating the odd message: 'Expected parameter declarator'
It seems as though only default constructors are supported in this scenario, or am I missing a bit of magic?
Currently using c++17 (simply because that's the default in this IDE)
class Fred
{
public:
Fred(const int i)
{
}
Fred()
{
}
};
Fred fred1(0); // This compiles
class Charlie
{
Fred fred2(); // This compiles
Fred fred3(0); // This does not compile
};

Initializing a variable using default constructor [duplicate]

This question already has answers here:
Default constructor with empty brackets
(9 answers)
Closed 7 years ago.
I'm pretty new to c++, i'm now trying to learn all the basics,
I know when default constructors are called, but when i tried different syntax it doesn't work like i expected.
Look at the following code:
class a;
class b();
class c(NULL);
'class' is a class i created with default constructor,
for a and c everything works well, but for b it just won't recognize the variable as a class member.
As i see it b and c are basically the same, what's wrong than?
Thanks!
Don't name your class "class", as it is a reserved name.
As for C++, if the constructor takes no parameters, you instantiate it using
Foo a; // note, if you are using c++11, you can do Foo a{};
As opposed to:
Foo b();
Which actually does something totally unexpected*, and declares a function named b that returns a Foo instance.
As for Foo c(null), it won't compile as there is no default constructor that takes an argument.
* It is referred to as "the most vexing parse", though I find that to be an exaggeration. It can certainly catch you by surprise, but just knowing that you can declare a function prototype inside a function, should be enough to remove the "vexing" aspect.
In other words int getMyInt(); is obviously a function prototype when placed outside any function definitions. However, since this is also the case when inside a function definition, int getMyInt(); doesn't do anything it wouldn't normally do... which is to define a function prototype getMyInt that returns an integer.
b is interpreted as a declaration of a function taking no arguments and returning an object of type class.
This is known as the most vexing parse. Edit: This is not the most vexing parse.

Class default constructor [duplicate]

This question already has answers here:
Two constructors, which is default?
(6 answers)
Closed 8 years ago.
Assuming that we have the class TestClass in our C++ project. A default constructor is the one empty parameters list. So we have:
TestClass();
TestClass(int defaultParam = 0);
Can these two be considered default constructors? And if they can be, is it ethical to have a default constructor like the second line?
Either of
TestClass(void);
TestClass(int defaultParam=0);
can be used as the default constructor. When you have both, it is a problem since the compiler cannot distinguish between the two when the compiler needs to use a default constructor. E.g.
TestClass anObject;
TestClass objectArray[5];
Unrelated to your question
For stylistic reasons, you should use:
TestClass();
instead of
TestClass(void);
The second form is supported by C++ but it's not necessary. The argument type void is necessary only when declaring functions in C.
having more than 1 constructor is called constructor overloading. If there are two default constructors it will generate an error as the compiler will not know which constructor to call while creating the object. If you don't declare a default constructor the complier does it by itself.

Constructors C++, What does the colon after a constructor mean? [duplicate]

This question already has answers here:
C++, What does the colon after a constructor mean? [duplicate]
(6 answers)
Closed 8 years ago.
Skimming through some code I noticed something I honestly can't wrap my head around in a constructor.
class Terrain
{
public:
Terrain(int movementCost,
bool isWater,
Texture texture)
: movementCost_(movementCost),
isWater_(isWater),
texture_(texture)
{}
... //More code
What sort of wizardry is this? Are those foo_(foo) representing foo = foo_?
This is a c++ initialiser list.
You have it almost right, foo_(foo) is equivalent to foo_ = foo;
This is useful for when you have a member variable that does not have a default constructor.
Without this feature, you would have to make it a pointer.
The initialisations are also executed in the order that the members were declared in the class defenition, not the order they appear in (which should be the same as a matter of style, but isn't necessarily)

Constructor setup? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is this weird colon-member syntax in the constructor?
I'm trying to understand what this kind of code means
Say I have this
class OptionStudent: public Student // derived class from Student class
{
public:
explicit OptionStudent(const std::string id = "12345678",
const std::string first = "someone")
: Student(id, first)
{
count_++;
}
}
What is that colon after the "someone"): <-- part called or mean for this constructor?
I know the constructor may be a little incorrect but I don't know what this is called. I just copied my notes from what the instructor was writing on the board and didn't understand it.
Something to do with the class or object remembering something?
It is the member initialization list. In this case, it calls the base class's constructor with id and first as arguments. It could also provide initial values for non-static data members of your class (if you had any).
Note that the semicolon after Student(id, first); is a syntax error and needs to be removed.
It is called an "initialization list". See following article "Understanding Initialization Lists in C++".
The basic idea is that when you enter the code of constructor after { you should have all members initialized to values passed as arguments or default.
Using initialization lists you can also pass arguments directly to base class too! This is what is happening in example you are describing:
first, both id and first are set to some values using default parameter value.
second, these values are used to initialize base Student class.
Of course one can pass different values as OptionStudent arguments and these values would be used to initialize Student.