C++ Call default constructor for Struct and its inherited members [duplicate]












0
















This question already has an answer here:




  • Why can I not brace initialize a struct derived from another struct?

    1 answer




I'd like to be able to initialize an object with the following syntax in C++14:



const auto data1 = DataOne{1, 2, 3};
const auto data2 = DataTwo{1, 2, 3, 4, 5};
const auto data3 = DataThree{1, 2, 3, 4, 5, 6, 7};


Which gives me the following error message:




error msg `error: no matching function for call to ‘DataThree::DataThree(<brace-enclosed initializer list>)’`



With the types defined as:



struct DataOne
{
int a;
int b;
int c;
};

struct DataTwo : DataOne
{
int d;
int e;
};

struct DataThree : DataTwo
{
int f;
int g;
};


I dont want to use the struct in struct method because then I will need to call params through double or triple dots which I dont want to use because all the members are equal important and it will look bad to read.










share|improve this question















marked as duplicate by Matthieu Brucher, rubenvb 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 26 '18 at 12:54


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.














  • 1





    So what's the error message? Which C++ standard can you use?

    – Matthieu Brucher
    Nov 26 '18 at 12:38











  • Perfectly valid C++17, there is nothing to reproduce.

    – Matthieu Brucher
    Nov 26 '18 at 12:41






  • 1





    A relevant question for you: stackoverflow.com/questions/16983539/…

    – bracco23
    Nov 26 '18 at 12:43


















0
















This question already has an answer here:




  • Why can I not brace initialize a struct derived from another struct?

    1 answer




I'd like to be able to initialize an object with the following syntax in C++14:



const auto data1 = DataOne{1, 2, 3};
const auto data2 = DataTwo{1, 2, 3, 4, 5};
const auto data3 = DataThree{1, 2, 3, 4, 5, 6, 7};


Which gives me the following error message:




error msg `error: no matching function for call to ‘DataThree::DataThree(<brace-enclosed initializer list>)’`



With the types defined as:



struct DataOne
{
int a;
int b;
int c;
};

struct DataTwo : DataOne
{
int d;
int e;
};

struct DataThree : DataTwo
{
int f;
int g;
};


I dont want to use the struct in struct method because then I will need to call params through double or triple dots which I dont want to use because all the members are equal important and it will look bad to read.










share|improve this question















marked as duplicate by Matthieu Brucher, rubenvb 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 26 '18 at 12:54


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.














  • 1





    So what's the error message? Which C++ standard can you use?

    – Matthieu Brucher
    Nov 26 '18 at 12:38











  • Perfectly valid C++17, there is nothing to reproduce.

    – Matthieu Brucher
    Nov 26 '18 at 12:41






  • 1





    A relevant question for you: stackoverflow.com/questions/16983539/…

    – bracco23
    Nov 26 '18 at 12:43
















0












0








0


0







This question already has an answer here:




  • Why can I not brace initialize a struct derived from another struct?

    1 answer




I'd like to be able to initialize an object with the following syntax in C++14:



const auto data1 = DataOne{1, 2, 3};
const auto data2 = DataTwo{1, 2, 3, 4, 5};
const auto data3 = DataThree{1, 2, 3, 4, 5, 6, 7};


Which gives me the following error message:




error msg `error: no matching function for call to ‘DataThree::DataThree(<brace-enclosed initializer list>)’`



With the types defined as:



struct DataOne
{
int a;
int b;
int c;
};

struct DataTwo : DataOne
{
int d;
int e;
};

struct DataThree : DataTwo
{
int f;
int g;
};


I dont want to use the struct in struct method because then I will need to call params through double or triple dots which I dont want to use because all the members are equal important and it will look bad to read.










share|improve this question

















This question already has an answer here:




  • Why can I not brace initialize a struct derived from another struct?

    1 answer




I'd like to be able to initialize an object with the following syntax in C++14:



const auto data1 = DataOne{1, 2, 3};
const auto data2 = DataTwo{1, 2, 3, 4, 5};
const auto data3 = DataThree{1, 2, 3, 4, 5, 6, 7};


Which gives me the following error message:




error msg `error: no matching function for call to ‘DataThree::DataThree(<brace-enclosed initializer list>)’`



With the types defined as:



struct DataOne
{
int a;
int b;
int c;
};

struct DataTwo : DataOne
{
int d;
int e;
};

struct DataThree : DataTwo
{
int f;
int g;
};


I dont want to use the struct in struct method because then I will need to call params through double or triple dots which I dont want to use because all the members are equal important and it will look bad to read.





This question already has an answer here:




  • Why can I not brace initialize a struct derived from another struct?

    1 answer








c++ initialization c++14






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 12:48









YSC

25.5k557112




25.5k557112










asked Nov 26 '18 at 12:37









PiodoPiodo

16612




16612




marked as duplicate by Matthieu Brucher, rubenvb 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 26 '18 at 12:54


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 Matthieu Brucher, rubenvb 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 26 '18 at 12:54


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.










  • 1





    So what's the error message? Which C++ standard can you use?

    – Matthieu Brucher
    Nov 26 '18 at 12:38











  • Perfectly valid C++17, there is nothing to reproduce.

    – Matthieu Brucher
    Nov 26 '18 at 12:41






  • 1





    A relevant question for you: stackoverflow.com/questions/16983539/…

    – bracco23
    Nov 26 '18 at 12:43
















  • 1





    So what's the error message? Which C++ standard can you use?

    – Matthieu Brucher
    Nov 26 '18 at 12:38











  • Perfectly valid C++17, there is nothing to reproduce.

    – Matthieu Brucher
    Nov 26 '18 at 12:41






  • 1





    A relevant question for you: stackoverflow.com/questions/16983539/…

    – bracco23
    Nov 26 '18 at 12:43










1




1





So what's the error message? Which C++ standard can you use?

– Matthieu Brucher
Nov 26 '18 at 12:38





So what's the error message? Which C++ standard can you use?

– Matthieu Brucher
Nov 26 '18 at 12:38













Perfectly valid C++17, there is nothing to reproduce.

– Matthieu Brucher
Nov 26 '18 at 12:41





Perfectly valid C++17, there is nothing to reproduce.

– Matthieu Brucher
Nov 26 '18 at 12:41




1




1





A relevant question for you: stackoverflow.com/questions/16983539/…

– bracco23
Nov 26 '18 at 12:43







A relevant question for you: stackoverflow.com/questions/16983539/…

– bracco23
Nov 26 '18 at 12:43














1 Answer
1






active

oldest

votes


















4














As of C++17, the syntax you wish for is valid:



const auto data3 = DataThree{1, 2, 3, 4, 5, 6, 7};


Live demo



Before that, aggregate initialization would be illegal per [dcl.init.aggr]/1:




An aggregate is an array or a class (Clause 9) with no user-provided constructors (12.1), no private or protected non-static data members (Clause 11), no base classes (Clause 10), and no virtual functions (10.3).







share|improve this answer






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4














    As of C++17, the syntax you wish for is valid:



    const auto data3 = DataThree{1, 2, 3, 4, 5, 6, 7};


    Live demo



    Before that, aggregate initialization would be illegal per [dcl.init.aggr]/1:




    An aggregate is an array or a class (Clause 9) with no user-provided constructors (12.1), no private or protected non-static data members (Clause 11), no base classes (Clause 10), and no virtual functions (10.3).







    share|improve this answer




























      4














      As of C++17, the syntax you wish for is valid:



      const auto data3 = DataThree{1, 2, 3, 4, 5, 6, 7};


      Live demo



      Before that, aggregate initialization would be illegal per [dcl.init.aggr]/1:




      An aggregate is an array or a class (Clause 9) with no user-provided constructors (12.1), no private or protected non-static data members (Clause 11), no base classes (Clause 10), and no virtual functions (10.3).







      share|improve this answer


























        4












        4








        4







        As of C++17, the syntax you wish for is valid:



        const auto data3 = DataThree{1, 2, 3, 4, 5, 6, 7};


        Live demo



        Before that, aggregate initialization would be illegal per [dcl.init.aggr]/1:




        An aggregate is an array or a class (Clause 9) with no user-provided constructors (12.1), no private or protected non-static data members (Clause 11), no base classes (Clause 10), and no virtual functions (10.3).







        share|improve this answer













        As of C++17, the syntax you wish for is valid:



        const auto data3 = DataThree{1, 2, 3, 4, 5, 6, 7};


        Live demo



        Before that, aggregate initialization would be illegal per [dcl.init.aggr]/1:




        An aggregate is an array or a class (Clause 9) with no user-provided constructors (12.1), no private or protected non-static data members (Clause 11), no base classes (Clause 10), and no virtual functions (10.3).








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 26 '18 at 12:40









        YSCYSC

        25.5k557112




        25.5k557112

















            Popular posts from this blog

            Create new schema in PostgreSQL using DBeaver

            Deepest pit of an array with Javascript: test on Codility

            Costa Masnaga