Questions tagged [in-class-initialization]
55 questions
35
votes
2 answers
Why to put val or var in kotlin class constructors
Just learning Kotlin In the first code down below there is the val keyword right in the other code there is not,
what is the different here if the val and var is omitted?
class Person(val firstName: String, val lastName: String) {
}
class…

Tord Larsen
- 2,670
- 8
- 33
- 76
25
votes
6 answers
python __init__ method in inherited class
I would like to give a daughter class some extra attributes without having to explicitly call a new method. So is there a way of giving the inherited class an __init__ type method which does not override the __init__ method of the parent class?
I…

Anake
- 7,201
- 12
- 45
- 59
25
votes
3 answers
What is the reason for not being able to deduce array size from initializer-string in member variable?
Consider the code:
struct Foo
{
const char str[] = "test";
};
int main()
{
Foo foo;
}
It fails to compile with both g++ and clang++, spitting out essentially
error: array bound cannot be deduced from an in-class initializer
I…

vsoftco
- 55,410
- 12
- 139
- 252
24
votes
2 answers
What is a C++11 extension [-Wc++11-extensions]
I need some help understanding where this error is occurring:
warning: in-class initialization of non-static data member is a C++11 extension [-Wc++11-extensions]
This is the section of the code that it is coming from:
typedef struct Hand {
…

Matthew Montefusco
- 241
- 1
- 2
- 3
19
votes
3 answers
C++11: in-class initializaton with "= {}" doesn't work with explicit constructor
In C++11 we can do in-class initialization using a "brace-or-equal-initializer" (words from the standard) like this:
struct Foo
{
/*explicit*/ Foo(int) {}
};
struct Bar
{
Foo foo = { 42 };
};
But if we un-comment explicit, it no longer…

John Zwinck
- 239,568
- 38
- 324
- 436
16
votes
5 answers
Bit-fields "In-class initialization" results in "error: lvalue required as left operand of assignment"
struct bitfield {
int i = 0; // ok
int j : 8 = 0; // error: lvalue required as left operand of assignment
};
What is the correct syntax to initialize bit-fields using C++11 "in-class initialization" feature?

iammilind
- 68,093
- 33
- 169
- 336
15
votes
1 answer
Inheriting-Constructors + In-Class-Initialization of non-default constructabe type fails
I am encountering the following error in my project:
error: use of deleted function ‘C::C(int)’ note: ‘C::C(int)’ is
implicitly deleted because the default definition would be ill-formed:
error: use of deleted function ‘M::M()’
This is the code I…

BCS
- 75,627
- 68
- 187
- 294
13
votes
2 answers
Why does in-class initialisation of static members violate the ODR?
There are several questions on Stack Overflow along the lines of "why can't I initialise static data members in-class in C++". Most answers quote from the standard telling you what you can do; those that attempt to answer why usually point to a link…

Tristan Brindle
- 16,281
- 4
- 39
- 82
13
votes
1 answer
User-declared default constructor + in-class initializers != user-provided constructor?
The Clang documentation neatly explains that
If a class or struct has no user-defined default constructor, C++
doesn't allow you to default construct a const instance of it like
this ([dcl.init], p9)
The rationale being that if a const object…

TemplateRex
- 69,038
- 19
- 164
- 304
12
votes
1 answer
Has "In class member initialization" feature made into C++11?
In class initialization feature, which allows to initialize normal members inside the class itself,
struct A {
int a = 0; // error: ISO C++ forbids in-class initialization of non-const static member ‘a’
};
This is giving error in latest compiler…

iammilind
- 68,093
- 33
- 169
- 336
12
votes
3 answers
How the try / catch in initialization list works?
We consider that an exception in initialization may happen. So we write try / catch block.
int f(){
throw 1;
}
class A
{
public:
A() try : _k(f())
{}
catch (int)
{
std::cout << "Exception 1" << std::endl;
…

Seagull
- 3,319
- 2
- 31
- 37
9
votes
2 answers
C++11 "In class initialization" feature is not working for unions
Minimal code example:
struct B {
union U {
struct S {} s;
int i = 100;
}
u;
};
Now if we declare a B obj; then the obj.u.i is assigned a garbage value instead of 100. See the demo here. (The garbage value differs based on…

iammilind
- 68,093
- 33
- 169
- 336
9
votes
1 answer
Why can't I make in-class initialized `const const std::string` a static member
I have the following working code:
#include
#include
class A {
public:
const std::string test = "42";
//static const std::string test = "42"; // fails
};
int main(void){
A a;
std::cout << a.test << '\n';
}
Is there a…

luk32
- 15,812
- 38
- 62
8
votes
2 answers
What is a right way to initialize fields in Spring Beans?
I'm wondering how should I initialize fields in Spring Beans? Here is several possible solutions:
1. Initialize fields directly on declaration
import org.springframework.stereotype.Component;
@Component
public class DeclarationInit {
private…

ytterrr
- 3,036
- 6
- 23
- 32
6
votes
2 answers
In-class initialization from static member of the same type
Is the following code valid, e.g. doesn't bring undefined behaviour?
struct S
{
int i = s.i;
static S s;
};
S S::s;
int main()
{
S a; // a.i = 0
S::s.i = 42;
S b; // b.i = 42
}
As far as I know all variables with static…

αλεχολυτ
- 4,792
- 1
- 35
- 71