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.
Related
This question already has answers here:
C++ : What is wrong with my class constructors ? (no default constructor exists &/or conflicting declaration)?
(2 answers)
Is it true that a default constructor is synthesized for every class that does not define one?
(6 answers)
What is a synthesized constructor?
(1 answer)
Closed 3 months ago.
constant variables and functions in the same program cpp
#include<bits/stdc++.h>
using namespace std;
class student
{
public:
const int roll;
const string name;
student (int r,string n)
:roll(r),name(n)
{
cout<<roll<<endl;
cout<<name<<endl;
}
void display()
{
cout<<"Disp\n";
}
};
int main()
{
student obj(2003081,"ismail");
student o; //ERROR
o.display();
return 0;
}
I can't understand, why the compiler shows "no matching function for call to 'student::student()' "?
Where is the problem and how can I overcome this?
When you write a class with a custom constructor (in your case, student (int r,string n), the compiler won't also generate a default constructor (i.e. one that doesn't expect any arguments, allowing student o; to compile). It probably wouldn't make sense to have a default constructor for your class, as you need to get values somehow to initialise the const member variables, as you do in your custom constructor. So, it's good that student o; is an error. That said, sometimes there are reasons to want a default-constructible type (for example, some container you want to use may insist on it). Then you have to get rid of the const members and have a way of setting them after the default constructor has run, which is a bit ugly!
You don't have a constructor without parameters, so you can't make an object without passing parameters. Overcome it, by either making a default constructor or not making an object without parameters.
The confusing part may be that you can make an object without parameters if you didn't make any constructor, but from the moment a constructor is made, the default constructor must also be made if needed. This can be done with:
student() : roll(0), name("");
Even though that probably doesn't make a lot of sense, so you're best in not using student o;.
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 ¶ms, Species *species ) : Ionization( params, species ) {
code
code which uses params and species
code
}
I understand that it is about IonizationTunnel constructor IonizationTunnel(Params ¶ms, 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.
This question already has answers here:
Initialization Order of Class Data Members
(2 answers)
Closed 4 years ago.
I have three class. First one is the 'Game', which is dependent to the other two. My second class is 'Window', which is a dependence for the third class 'VKBase'. Those last two classes have no parameters in their constructor functions. Game contains the others as object members, and Window have to get initialised before VKBase.
class Game
{
Boolean bit = true;
Window window;
VKBase VkBase;
public:
Game();
~Game();
void _Set();
void Loop();
void Exit();
};
Because that these two class not have parameters in their constructor functions, i can't initialise them in the constructor function of the Game. So they will get initialised whenever i create an object from the class Game.
But the question is, since initialisation order is important, which one will get initialised first? The window or the VkBase?
Initialization order is always left-to-right: first class bases, left-to-right, then member variables in declaration order. So the first one to be initialized in your class is bit, and then follows window.
In which order object class members [...] are initialized?
In the order in which they were declared. In your example, bit is initialized first, window second, VkBase third. Whether these data members have a default constructor or one depending on parameters doesn't matter here.
Because that these two class not have parameters in their constructor functions, i can't initialise them in the constructor function of the Game
You kind got that wrong, you do in fact initialize the instances by calling the default constructor. Note that for any class that has a default constructor,
ClassWithDefaultCtor instance;
does call this default constructor when this variable is instantiated (might be when an enclosing class is constructed, if the above declaration is a class data member). This syntax is called default initialization, and this is exactly what happens in your class. Note that you can change the above to
ClassWithDefaultCtor instance{};
which might make it clearer that the object is properly initialized.
This question already has answers here:
When do we need to have a default constructor?
(7 answers)
Closed 8 years ago.
I have the following classes:
class ArithmeticExpression
{
public:
ArithmeticExpression(std::string expr);
}
class Command
{
public:
Command(){};
//this is a virtual class
}
class CommandAssign : public Command
{
private:
ArithmeticExpression expr;
public:
CommandAssign();
CommandAssign(ArithmeticExpression);
}
Now when I try to write the constructor for the CommandAssign class as in:
CommandAssign::CommandAssign(ArithmeticExpression expr)
:Command()
{
this -> expr = ArithmeticExpression(expr.getExpr());
}
I get the error:
no matching function for call to ‘ArithmeticExpression::ArithmeticExpression()’
:Command()
Apparently I can fix that by adding an empty constructor in ArithmeticExpression class that does not do anything. What is it so special about this empty constructor that makes it work? I do not explicitly call anywhere. Do you always need to define an empty constructor in C++?
I wanted to emphasize that although from the title it seems that my question is similar to the one some users suggested as being a duplicate of, the answer I was looking for is NOT there. I was simply trying to understand what happens when a constructor is called and how to avoid defining a useless default constructor, which I knew already is not automatically defined by the compiler in the case where I define one with parameters.
A default constructor will only be automatically generated by the compiler if no other constructors are defined.
EDIT:
The default constructor is needed for object initialization.
All members are initialised before the constructor body begins. If one doesn't have an entry in the initialiser list, then it will be default-initialised; but this is only possible (for a class type) if it has a default constructor.
expr is not initialised in the initialiser list, and doesn't have a default constructor (since declaring any constructor prevents one from being implicitly generated), so it can't be initialised - hence the error.
You should initialise it in the list, rather than reassigning it in the constructor body:
CommandAssign::CommandAssign(ArithmeticExpression expr) :
expr(expr.getExpr())
{}
Note that there's no need to explicitly default-construct the Command sub-object. This also requires the constructor of ArithmeticExpression to be public: it's private in your example code.
This question already has answers here:
What is this weird colon-member (" : ") syntax in the constructor?
(14 answers)
Closed 9 years ago.
Please could someone explain to me what this operator does in C++ at a function?
class simplecanny
{
ros::NodeHandle nh_;
ros::NodeHandle n;
ros::Publisher pub ;
image_transport::ImageTransport it_;
image_transport::Subscriber image_sub_; //image subscriber
image_transport::Publisher image_pub_; //image publisher(we subscribe to ardrone image_raw)
std_msgs::String msg;
public:
*** simplecanny()
: it_(nh_) ***
{
image_sub_ = it_.subscribe("/ardrone/image_raw", 1, &simplecanny::imageCb, this);
image_pub_= it_.advertise("/arcv/Image",1);
}
~simplecanny()
{
cv::destroyWindow(WINDOW);
}
...
At the simplecanny() : it_(nh_) constructor, Im not familiar with the : it_(nh_) part. What does it do? Is that a case of operator overloading?
Thanks in advance!
This is called the constructor initializer list. It gives the parameters to be passed to the constructors of the base classes and members of the class.
In your example, it is passing nh_ to the constructor of it_.
Any base class or member that does not appear in this list is constructed using their respective default constructors.
Call the superclass constructor in the subclass' initialization list.
The single colon (:) is no operator but a part of the language, indication the start of an initialization list. The initialization list can be used only in constructors and is used to initialize the object's member variables and superclass subobjects. In your case, the member variable it_ is initialized with nh_. You might want to look up initialization lists and constructors in the texbook of your choice.
It is a constructor initialization list. You can read more on the subject at In this specific case, is there a difference between using a member initializer list and assigning values in a constructor?
It is the member-initialization-list. It permits to pass the correct parameters and chose the good constructor for the members of the class and the constructor of the base classes.
The standard says:
12.6.2 Initializing bases and members [class.base.init]
In the definition of a constructor for a class, initializers for direct and virtual base subobjects and non-static data members can be specified by a ctor-initializer, which has the form
ctor-initializer:
: mem-initializer-list
Any members or base class not specified in the member-initialization-list will use its default constructor.
In you case, you are passing nh_ to the constructor of image_transport::ImageTransport to initialize it_.
The column represents a start of an initialization list. It is used for setting up the variables of objects. Its another important and useful feature is invocation of desired constructor of based class which this class derives. The detailed answer and reason why it is introduced to C++ can be found at Constructor initialization lists and invocation of desired constructor of based class