Are there 2 times initialization when there is a constructor with default argument [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











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?










share|improve this question















marked as duplicate by StoryTeller c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

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














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?










share|improve this question















marked as duplicate by StoryTeller c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

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












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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

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 c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

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












  • 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












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.






share|improve this answer



























    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.







    share|improve this answer



























      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.






      share|improve this answer



























        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.






        share|improve this answer
























          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.






          share|improve this answer






















            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.






            share|improve this answer












            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.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 20 at 15:50









            NathanOliver

            82.8k15112172




            82.8k15112172






















                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.







                share|improve this answer
























                  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.







                  share|improve this answer






















                    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.







                    share|improve this answer












                    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.








                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 20 at 15:50









                    songyuanyao

                    88.5k11170232




                    88.5k11170232




















                        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.






                        share|improve this answer
























                          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.






                          share|improve this answer






















                            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.






                            share|improve this answer












                            = "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.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 20 at 15:54









                            François Andrieux

                            14.8k32345




                            14.8k32345












                                Popular posts from this blog

                                How to check contact read email or not when send email to Individual?

                                Bahrain

                                Postfix configuration issue with fips on centos 7; mailgun relay