Are there 2 times initialization when there is a constructor with default argument [duplicate]
up vote
7
down vote
favorite
This question already has an answer here:
C++ Initialising fields directly vs initialisation list in default constructor
3 answers
My question is about how the member data with initializer get initialized where there is also a default argument in the constructor.
class InputPlay {
public:
InputPlay(std::string s = "test" ) : _s(s) { };
private:
std::string _s = "default";
};
Question:
Is there are going to be 2 times initialization for the variable _s when the construct is called? aka the _s will be initialized by the string literal default and then replace by the default argument "test" in the constructor?
c++ constructor initialization
marked as duplicate by StoryTeller
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 at 16:35
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
up vote
7
down vote
favorite
This question already has an answer here:
C++ Initialising fields directly vs initialisation list in default constructor
3 answers
My question is about how the member data with initializer get initialized where there is also a default argument in the constructor.
class InputPlay {
public:
InputPlay(std::string s = "test" ) : _s(s) { };
private:
std::string _s = "default";
};
Question:
Is there are going to be 2 times initialization for the variable _s when the construct is called? aka the _s will be initialized by the string literal default and then replace by the default argument "test" in the constructor?
c++ constructor initialization
marked as duplicate by StoryTeller
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 at 16:35
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
3
Off-topic: As you do provide the argument by value (so a new object is created anyway), you might avoid copying this object into_s
in favour of moving it:: _s(std::move(s))
.
– Aconcagua
Nov 20 at 15:55
@Aconcagua very nice off-topic comments!
– SLN
Nov 20 at 16:24
add a comment |
up vote
7
down vote
favorite
up vote
7
down vote
favorite
This question already has an answer here:
C++ Initialising fields directly vs initialisation list in default constructor
3 answers
My question is about how the member data with initializer get initialized where there is also a default argument in the constructor.
class InputPlay {
public:
InputPlay(std::string s = "test" ) : _s(s) { };
private:
std::string _s = "default";
};
Question:
Is there are going to be 2 times initialization for the variable _s when the construct is called? aka the _s will be initialized by the string literal default and then replace by the default argument "test" in the constructor?
c++ constructor initialization
This question already has an answer here:
C++ Initialising fields directly vs initialisation list in default constructor
3 answers
My question is about how the member data with initializer get initialized where there is also a default argument in the constructor.
class InputPlay {
public:
InputPlay(std::string s = "test" ) : _s(s) { };
private:
std::string _s = "default";
};
Question:
Is there are going to be 2 times initialization for the variable _s when the construct is called? aka the _s will be initialized by the string literal default and then replace by the default argument "test" in the constructor?
This question already has an answer here:
C++ Initialising fields directly vs initialisation list in default constructor
3 answers
c++ constructor initialization
c++ constructor initialization
edited Nov 21 at 5:36
Cœur
17k9102140
17k9102140
asked Nov 20 at 15:46
SLN
1,4911429
1,4911429
marked as duplicate by StoryTeller
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 at 16:35
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by StoryTeller
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 at 16:35
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
3
Off-topic: As you do provide the argument by value (so a new object is created anyway), you might avoid copying this object into_s
in favour of moving it:: _s(std::move(s))
.
– Aconcagua
Nov 20 at 15:55
@Aconcagua very nice off-topic comments!
– SLN
Nov 20 at 16:24
add a comment |
3
Off-topic: As you do provide the argument by value (so a new object is created anyway), you might avoid copying this object into_s
in favour of moving it:: _s(std::move(s))
.
– Aconcagua
Nov 20 at 15:55
@Aconcagua very nice off-topic comments!
– SLN
Nov 20 at 16:24
3
3
Off-topic: As you do provide the argument by value (so a new object is created anyway), you might avoid copying this object into
_s
in favour of moving it: : _s(std::move(s))
.– Aconcagua
Nov 20 at 15:55
Off-topic: As you do provide the argument by value (so a new object is created anyway), you might avoid copying this object into
_s
in favour of moving it: : _s(std::move(s))
.– Aconcagua
Nov 20 at 15:55
@Aconcagua very nice off-topic comments!
– SLN
Nov 20 at 16:24
@Aconcagua very nice off-topic comments!
– SLN
Nov 20 at 16:24
add a comment |
3 Answers
3
active
oldest
votes
up vote
9
down vote
accepted
No, _s
will only be initialized once. The in class initialization is syntactic sugar for synthesizing a member initializer. If you provide your own member initializer then the compiler will use that instead of synthesizing one for you from the in class initialization.
add a comment |
up vote
6
down vote
No. For non-static data member, when both default member initializer and member initializer list are provided, the default member initializer will be ignored. That means _s
will be initialized by the argument s
of the constructor directly.
If a member has a default member initializer and also appears in the member initialization list in a constructor, the default member initializer is ignored.
add a comment |
up vote
5
down vote
= "default";
is a default member initializer. It's the initializer to use for _s
if you don't provide one. But since you do provide one (_s(s)
) the default initializer never comes into player and _s
is simply initialized to s
.
From cppreference.com :
If a non-static data member has an default member initializer and also appears in a member initializer list, then member initializer list is executed and the default member initializer is ignored:
Note that by definition something can only ever be initialized once. If something looks like it's initialized twice, it's usually being initialized and then assigned another value.
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
9
down vote
accepted
No, _s
will only be initialized once. The in class initialization is syntactic sugar for synthesizing a member initializer. If you provide your own member initializer then the compiler will use that instead of synthesizing one for you from the in class initialization.
add a comment |
up vote
9
down vote
accepted
No, _s
will only be initialized once. The in class initialization is syntactic sugar for synthesizing a member initializer. If you provide your own member initializer then the compiler will use that instead of synthesizing one for you from the in class initialization.
add a comment |
up vote
9
down vote
accepted
up vote
9
down vote
accepted
No, _s
will only be initialized once. The in class initialization is syntactic sugar for synthesizing a member initializer. If you provide your own member initializer then the compiler will use that instead of synthesizing one for you from the in class initialization.
No, _s
will only be initialized once. The in class initialization is syntactic sugar for synthesizing a member initializer. If you provide your own member initializer then the compiler will use that instead of synthesizing one for you from the in class initialization.
answered Nov 20 at 15:50
NathanOliver
82.7k15112172
82.7k15112172
add a comment |
add a comment |
up vote
6
down vote
No. For non-static data member, when both default member initializer and member initializer list are provided, the default member initializer will be ignored. That means _s
will be initialized by the argument s
of the constructor directly.
If a member has a default member initializer and also appears in the member initialization list in a constructor, the default member initializer is ignored.
add a comment |
up vote
6
down vote
No. For non-static data member, when both default member initializer and member initializer list are provided, the default member initializer will be ignored. That means _s
will be initialized by the argument s
of the constructor directly.
If a member has a default member initializer and also appears in the member initialization list in a constructor, the default member initializer is ignored.
add a comment |
up vote
6
down vote
up vote
6
down vote
No. For non-static data member, when both default member initializer and member initializer list are provided, the default member initializer will be ignored. That means _s
will be initialized by the argument s
of the constructor directly.
If a member has a default member initializer and also appears in the member initialization list in a constructor, the default member initializer is ignored.
No. For non-static data member, when both default member initializer and member initializer list are provided, the default member initializer will be ignored. That means _s
will be initialized by the argument s
of the constructor directly.
If a member has a default member initializer and also appears in the member initialization list in a constructor, the default member initializer is ignored.
answered Nov 20 at 15:50
songyuanyao
88.4k11170232
88.4k11170232
add a comment |
add a comment |
up vote
5
down vote
= "default";
is a default member initializer. It's the initializer to use for _s
if you don't provide one. But since you do provide one (_s(s)
) the default initializer never comes into player and _s
is simply initialized to s
.
From cppreference.com :
If a non-static data member has an default member initializer and also appears in a member initializer list, then member initializer list is executed and the default member initializer is ignored:
Note that by definition something can only ever be initialized once. If something looks like it's initialized twice, it's usually being initialized and then assigned another value.
add a comment |
up vote
5
down vote
= "default";
is a default member initializer. It's the initializer to use for _s
if you don't provide one. But since you do provide one (_s(s)
) the default initializer never comes into player and _s
is simply initialized to s
.
From cppreference.com :
If a non-static data member has an default member initializer and also appears in a member initializer list, then member initializer list is executed and the default member initializer is ignored:
Note that by definition something can only ever be initialized once. If something looks like it's initialized twice, it's usually being initialized and then assigned another value.
add a comment |
up vote
5
down vote
up vote
5
down vote
= "default";
is a default member initializer. It's the initializer to use for _s
if you don't provide one. But since you do provide one (_s(s)
) the default initializer never comes into player and _s
is simply initialized to s
.
From cppreference.com :
If a non-static data member has an default member initializer and also appears in a member initializer list, then member initializer list is executed and the default member initializer is ignored:
Note that by definition something can only ever be initialized once. If something looks like it's initialized twice, it's usually being initialized and then assigned another value.
= "default";
is a default member initializer. It's the initializer to use for _s
if you don't provide one. But since you do provide one (_s(s)
) the default initializer never comes into player and _s
is simply initialized to s
.
From cppreference.com :
If a non-static data member has an default member initializer and also appears in a member initializer list, then member initializer list is executed and the default member initializer is ignored:
Note that by definition something can only ever be initialized once. If something looks like it's initialized twice, it's usually being initialized and then assigned another value.
answered Nov 20 at 15:54
François Andrieux
14.8k32345
14.8k32345
add a comment |
add a comment |
3
Off-topic: As you do provide the argument by value (so a new object is created anyway), you might avoid copying this object into
_s
in favour of moving it:: _s(std::move(s))
.– Aconcagua
Nov 20 at 15:55
@Aconcagua very nice off-topic comments!
– SLN
Nov 20 at 16:24