This question already has answers here:
default arguments in constructor
(5 answers)
Closed 9 years ago.
Is it possible to make constructor with default arguments.
Something like this
Object(int size = 1) {
//SMTH
}
I am trying to do like this in visual studio, but error apears.
Is it possible to create constructor with default arguments or it only remains to use overloading ?
EDIT
It is possible, sorry for posting this question, I am newbie in c++. The problem was that I was declaring default argument in source file (cpp) , I have changed my class like this and it works great.
class MyClass
{
public:
MyClass (int i = 0, std::string s = ""); // constructor declared
private:
int x;
int y;
std::string z;
};
MyClass :: MyClass(int i, std::string s) // constructor defined
{
x = 100;
y = i;
z = s;
}
Yes, it is possible to have constructors with default arguments.
Two caveats regarding your particular example:
Make sure you don't also have a no-args constructor as this would lead to ambiguity.
You might want to make this constructor explicit so that it's not used for implicit conversion from int to Object.
Related
This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
Variables After the Colon in a Constructor
C++ constructor syntax question (noob)
I have some C++ code here:
class demo
{
private:
unsigned char len, *dat;
public:
demo(unsigned char le = 5, unsigned char default) : len(le)
{
dat = new char[len];
for (int i = 0; i <= le; i++)
dat[i] = default;
}
void ~demo(void)
{
delete [] *dat;
}
};
class newdemo : public demo
{
private:
int *dat1;
public:
newdemo(void) : demo(0, 0)
{
*dat1 = 0;
return 0;
}
};
My question is, what are the : len(le) and : demo(0, 0) called?
Is it something to do with inheritance?
As others have said, it's an initialisation list. You can use it for two things:
Calling base class constructors
Initialising member variables before the body of the constructor executes.
For case #1, I assume you understand inheritance (if that's not the case, let me know in the comments). So you are simply calling the constructor of your base class.
For case #2, the question may be asked: "Why not just initialise it in the body of the constructor?" The importance of the initialisation lists is particularly evident for const members. For instance, take a look at this situation, where I want to initialise m_val based on the constructor parameter:
class Demo
{
Demo(int& val)
{
m_val = val;
}
private:
const int& m_val;
};
By the C++ specification, this is illegal. We cannot change the value of a const variable in the constructor, because it is marked as const. So you can use the initialisation list:
class Demo
{
Demo(int& val) : m_val(val)
{
}
private:
const int& m_val;
};
That is the only time that you can change a const member variable. And as Michael noted in the comments section, it is also the only way to initialise a reference that is a class member.
Outside of using it to initialise const member variables, it seems to have been generally accepted as "the way" of initialising variables, so it's clear to other programmers reading your code.
This is called an initialization list. It is for passing arguments to the constructor of a parent class. Here is a good link explaining it: Initialization Lists in C++
It's called an initialization list. It initializes members before the body of the constructor executes.
It's called an initialization list. An initializer list is how you pass arguments to your member variables' constructors and for passing arguments to the parent class's constructor.
If you use = to assign in the constructor body, first the default constructor is called, then the assignment operator is called. This is a bit wasteful, and sometimes there's no equivalent assignment operator.
It means that len is not set using the default constructor. while the demo class is being constructed. For instance:
class Demo{
int foo;
public:
Demo(){ foo = 1;}
};
Would first place a value in foo before setting it to 1. It's slightly faster and more efficient.
You are calling the constructor of its base class, demo.
This question already has answers here:
Which is the difference between declaring a constructor private and =delete?
(7 answers)
Closed 7 years ago.
Let us assume we have a class X and we want wo explicitly forbid, let say the standard constructor. I used for a long time in the Header file:
private:
X(); // 1.
so, that the contructor was disabled outside the class, so for anybody. But recently I have learned that in C++11 follwoing was recommended:
X() = delete; // 2.
Both would achive my wish to forbid the standard contructor.
But what is the exact difference bewteen them? Why would C++11 recommend the last one? Are there any other flags, signals set in the 2. way?
Example 1 was the way to do it before we had = delete which came out in C++11. Now that we have = delete this will completely get rid of the constructor. With making the constructor private you could still use that constructor in a member function where if you try to default an object in a member function with = delete it will be a compiler error.
#include <iostream>
class Foo
{
Foo();
public:
static void SomeFunc() { Foo f; }
};
class Bar
{
public:
Bar() = delete;
static void SomeFunc() { Bar b; }
};
int main()
{
Foo::SomeFunc(); // will compile
Bar::SomeFunc(); // compiler error
}
Live Example
The former is kind of a hack. A friend class could still call the constructor (and you'd get an error at link-time unless you actually defined it as well).
The latter actually prevents its auto-generation so it's really not there.
This question already has answers here:
no default constructor exists for class x (inheritance) C++
(2 answers)
Default constructor for an inherited class
(2 answers)
Closed 8 years ago.
I have just started to learn inheritance in c++. I have question about it. Here is my code:
class employ
{
int xelfasi;
string manqana;
public:
employ(istream&is)
{
is >> xelfasi >> manqana;
}
~employ()
{
}
};
class manager : public employ
{
int bonusi;
manager(int x)
{
bonusi = x;
}
};
which gives me an error saying: no default constructor exists for class "employ".
I don't really get why I have to make an default constructor. Thanks
manager basically contains an employ (though in the form of inheritance). So to be able to construct a manager, you need to be able to construct the contained employ. So you have to either provide a default constructor for it, or construct it explicitly by calling an existing constructor.
To construct it explicitly, you have to do something like this:
manager(int x): employ(#some istream here#)
{}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why should I prefer to use member initialization list?
Class A has a member variable i. i can be initialized or assigned during object creation.
A) Initialise
class A {
int i;
public:
A(int _i) : i(_i){}
}
B) assign
class A {
int i;
public:
A(int _i) : { i = _i}
}
My question is what is the basic difference between these 2 approach?
The difference lies in which C++ mechanism is used to initialize i in your class. Case (A) initializes it via constructor, and case (B) uses the assignment operator (or a copy constructor if no assignment operator is defined).
Most C++ compilers would generate exactly the same code for this particular example, because you're using int, which is a "plain old data" type. If i were a class type, it could make a great deal of difference.
This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
Variables After the Colon in a Constructor
C++ constructor syntax question (noob)
I have some C++ code here:
class demo
{
private:
unsigned char len, *dat;
public:
demo(unsigned char le = 5, unsigned char default) : len(le)
{
dat = new char[len];
for (int i = 0; i <= le; i++)
dat[i] = default;
}
void ~demo(void)
{
delete [] *dat;
}
};
class newdemo : public demo
{
private:
int *dat1;
public:
newdemo(void) : demo(0, 0)
{
*dat1 = 0;
return 0;
}
};
My question is, what are the : len(le) and : demo(0, 0) called?
Is it something to do with inheritance?
As others have said, it's an initialisation list. You can use it for two things:
Calling base class constructors
Initialising member variables before the body of the constructor executes.
For case #1, I assume you understand inheritance (if that's not the case, let me know in the comments). So you are simply calling the constructor of your base class.
For case #2, the question may be asked: "Why not just initialise it in the body of the constructor?" The importance of the initialisation lists is particularly evident for const members. For instance, take a look at this situation, where I want to initialise m_val based on the constructor parameter:
class Demo
{
Demo(int& val)
{
m_val = val;
}
private:
const int& m_val;
};
By the C++ specification, this is illegal. We cannot change the value of a const variable in the constructor, because it is marked as const. So you can use the initialisation list:
class Demo
{
Demo(int& val) : m_val(val)
{
}
private:
const int& m_val;
};
That is the only time that you can change a const member variable. And as Michael noted in the comments section, it is also the only way to initialise a reference that is a class member.
Outside of using it to initialise const member variables, it seems to have been generally accepted as "the way" of initialising variables, so it's clear to other programmers reading your code.
This is called an initialization list. It is for passing arguments to the constructor of a parent class. Here is a good link explaining it: Initialization Lists in C++
It's called an initialization list. It initializes members before the body of the constructor executes.
It's called an initialization list. An initializer list is how you pass arguments to your member variables' constructors and for passing arguments to the parent class's constructor.
If you use = to assign in the constructor body, first the default constructor is called, then the assignment operator is called. This is a bit wasteful, and sometimes there's no equivalent assignment operator.
It means that len is not set using the default constructor. while the demo class is being constructed. For instance:
class Demo{
int foo;
public:
Demo(){ foo = 1;}
};
Would first place a value in foo before setting it to 1. It's slightly faster and more efficient.
You are calling the constructor of its base class, demo.