Why doesn't a class having private constructor prevent inheriting from this class? How to control which classes can inherit from a certain base?C++ zero initialization - Why is `b` in this program uninitialized, but `a` is initialized?Deleted default constructor. Objects can still be created… sometimesWhat is the default access of constructor in c++Extending a singleton class which has private constructor and destructor gives compile time warningHow to Restrict creation of objects from certain classCan a friend class invoke a private constructor in c++? (and what's Singleton)how can a base class disable the derived class's constructorCan you force classes inheriting from abstract base class to only have the public methods defined in base case?Inherit from class with private initializer?How to enforce private constructors in children of a base classConditional access to base class from inherited class in c++How to unit test a class with private constructor?Why does C++ forbid private inheritance of a final class?

Why does Kotter return in Welcome Back Kotter?

How old can references or sources in a thesis be?

Roll the carpet

Which country benefited the most from UN Security Council vetoes?

Convert two switches to a dual stack, and add outlet - possible here?

Languages that we cannot (dis)prove to be Context-Free

What is a clear way to write a bar that has an extra beat?

What would happen to a modern skyscraper if it rains micro blackholes?

Was any UN Security Council vote triple-vetoed?

Mortgage Pre-approval / Loan - Apply Alone or with Fiancée?

What doth I be?

expand `ifthenelse` immediately

Why is Minecraft giving an OpenGL error?

Are the number of citations and number of published articles the most important criteria for a tenure promotion?

How can bays and straits be determined in a procedurally generated map?

Could an aircraft fly or hover using only jets of compressed air?

Are astronomers waiting to see something in an image from a gravitational lens that they've already seen in an adjacent image?

Maximum likelihood parameters deviate from posterior distributions

Perform and show arithmetic with LuaLaTeX

How to format long polynomial?

Did Shadowfax go to Valinor?

How much of data wrangling is a data scientist's job?

Why is consensus so controversial in Britain?

What's that red-plus icon near a text?



Why doesn't a class having private constructor prevent inheriting from this class? How to control which classes can inherit from a certain base?


C++ zero initialization - Why is `b` in this program uninitialized, but `a` is initialized?Deleted default constructor. Objects can still be created… sometimesWhat is the default access of constructor in c++Extending a singleton class which has private constructor and destructor gives compile time warningHow to Restrict creation of objects from certain classCan a friend class invoke a private constructor in c++? (and what's Singleton)how can a base class disable the derived class's constructorCan you force classes inheriting from abstract base class to only have the public methods defined in base case?Inherit from class with private initializer?How to enforce private constructors in children of a base classConditional access to base class from inherited class in c++How to unit test a class with private constructor?Why does C++ forbid private inheritance of a final class?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








24















class B 
private:
friend class C;
B() = default;
;

class C : public B ;
class D : public B ;

int main()
C ;
D ;
return 0;



I assumed that since only class C is a friend of B, and B's constructor is private, then only class C is valid and D is not allowed to instantiate B. But that's not how it works. Where am I wrong with my reasoning, and how to achieve this kind of control over which classes are allowed to subclass a certain base?



Update: as pointed out by others in the comments, the snippet above works as I initially expected under C++14, but not C++17. Changing the instantiation to C c; D d; in main() does work as expected in C++17 mode as well.










share|improve this question
























  • See this: stackoverflow.com/questions/32235294/…

    – Diodacus
    9 hours ago











  • @Diodacus: so what, declaring a private constructor as default makes it public, despite being declared in the private: section?

    – Violet Giraffe
    9 hours ago






  • 2





    I got the error you expect: "'D::D(void)': attempting to reference a deleted function" (msvs 2017)

    – vahancho
    9 hours ago











  • how to achieve this kind of control over which classes are allowed to sublcass a certain base; I understand your wish, but can you explain why you would want this? Because it means that whenever you want to make an extra class, through inheritance, you need to alter the base class and this might be considered as anti-pattern

    – Stefan
    9 hours ago






  • 1





    @Stefan: I hear you, but there are exactly two classes for which it is semantically meaningful to subclass B, and I'm trying to express/enforce this logical constraint in C++.

    – Violet Giraffe
    9 hours ago


















24















class B 
private:
friend class C;
B() = default;
;

class C : public B ;
class D : public B ;

int main()
C ;
D ;
return 0;



I assumed that since only class C is a friend of B, and B's constructor is private, then only class C is valid and D is not allowed to instantiate B. But that's not how it works. Where am I wrong with my reasoning, and how to achieve this kind of control over which classes are allowed to subclass a certain base?



Update: as pointed out by others in the comments, the snippet above works as I initially expected under C++14, but not C++17. Changing the instantiation to C c; D d; in main() does work as expected in C++17 mode as well.










share|improve this question
























  • See this: stackoverflow.com/questions/32235294/…

    – Diodacus
    9 hours ago











  • @Diodacus: so what, declaring a private constructor as default makes it public, despite being declared in the private: section?

    – Violet Giraffe
    9 hours ago






  • 2





    I got the error you expect: "'D::D(void)': attempting to reference a deleted function" (msvs 2017)

    – vahancho
    9 hours ago











  • how to achieve this kind of control over which classes are allowed to sublcass a certain base; I understand your wish, but can you explain why you would want this? Because it means that whenever you want to make an extra class, through inheritance, you need to alter the base class and this might be considered as anti-pattern

    – Stefan
    9 hours ago






  • 1





    @Stefan: I hear you, but there are exactly two classes for which it is semantically meaningful to subclass B, and I'm trying to express/enforce this logical constraint in C++.

    – Violet Giraffe
    9 hours ago














24












24








24


1






class B 
private:
friend class C;
B() = default;
;

class C : public B ;
class D : public B ;

int main()
C ;
D ;
return 0;



I assumed that since only class C is a friend of B, and B's constructor is private, then only class C is valid and D is not allowed to instantiate B. But that's not how it works. Where am I wrong with my reasoning, and how to achieve this kind of control over which classes are allowed to subclass a certain base?



Update: as pointed out by others in the comments, the snippet above works as I initially expected under C++14, but not C++17. Changing the instantiation to C c; D d; in main() does work as expected in C++17 mode as well.










share|improve this question
















class B 
private:
friend class C;
B() = default;
;

class C : public B ;
class D : public B ;

int main()
C ;
D ;
return 0;



I assumed that since only class C is a friend of B, and B's constructor is private, then only class C is valid and D is not allowed to instantiate B. But that's not how it works. Where am I wrong with my reasoning, and how to achieve this kind of control over which classes are allowed to subclass a certain base?



Update: as pointed out by others in the comments, the snippet above works as I initially expected under C++14, but not C++17. Changing the instantiation to C c; D d; in main() does work as expected in C++17 mode as well.







c++ c++11 inheritance c++17






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 9 hours ago







Violet Giraffe

















asked 9 hours ago









Violet GiraffeViolet Giraffe

15k29139256




15k29139256












  • See this: stackoverflow.com/questions/32235294/…

    – Diodacus
    9 hours ago











  • @Diodacus: so what, declaring a private constructor as default makes it public, despite being declared in the private: section?

    – Violet Giraffe
    9 hours ago






  • 2





    I got the error you expect: "'D::D(void)': attempting to reference a deleted function" (msvs 2017)

    – vahancho
    9 hours ago











  • how to achieve this kind of control over which classes are allowed to sublcass a certain base; I understand your wish, but can you explain why you would want this? Because it means that whenever you want to make an extra class, through inheritance, you need to alter the base class and this might be considered as anti-pattern

    – Stefan
    9 hours ago






  • 1





    @Stefan: I hear you, but there are exactly two classes for which it is semantically meaningful to subclass B, and I'm trying to express/enforce this logical constraint in C++.

    – Violet Giraffe
    9 hours ago


















  • See this: stackoverflow.com/questions/32235294/…

    – Diodacus
    9 hours ago











  • @Diodacus: so what, declaring a private constructor as default makes it public, despite being declared in the private: section?

    – Violet Giraffe
    9 hours ago






  • 2





    I got the error you expect: "'D::D(void)': attempting to reference a deleted function" (msvs 2017)

    – vahancho
    9 hours ago











  • how to achieve this kind of control over which classes are allowed to sublcass a certain base; I understand your wish, but can you explain why you would want this? Because it means that whenever you want to make an extra class, through inheritance, you need to alter the base class and this might be considered as anti-pattern

    – Stefan
    9 hours ago






  • 1





    @Stefan: I hear you, but there are exactly two classes for which it is semantically meaningful to subclass B, and I'm trying to express/enforce this logical constraint in C++.

    – Violet Giraffe
    9 hours ago

















See this: stackoverflow.com/questions/32235294/…

– Diodacus
9 hours ago





See this: stackoverflow.com/questions/32235294/…

– Diodacus
9 hours ago













@Diodacus: so what, declaring a private constructor as default makes it public, despite being declared in the private: section?

– Violet Giraffe
9 hours ago





@Diodacus: so what, declaring a private constructor as default makes it public, despite being declared in the private: section?

– Violet Giraffe
9 hours ago




2




2





I got the error you expect: "'D::D(void)': attempting to reference a deleted function" (msvs 2017)

– vahancho
9 hours ago





I got the error you expect: "'D::D(void)': attempting to reference a deleted function" (msvs 2017)

– vahancho
9 hours ago













how to achieve this kind of control over which classes are allowed to sublcass a certain base; I understand your wish, but can you explain why you would want this? Because it means that whenever you want to make an extra class, through inheritance, you need to alter the base class and this might be considered as anti-pattern

– Stefan
9 hours ago





how to achieve this kind of control over which classes are allowed to sublcass a certain base; I understand your wish, but can you explain why you would want this? Because it means that whenever you want to make an extra class, through inheritance, you need to alter the base class and this might be considered as anti-pattern

– Stefan
9 hours ago




1




1





@Stefan: I hear you, but there are exactly two classes for which it is semantically meaningful to subclass B, and I'm trying to express/enforce this logical constraint in C++.

– Violet Giraffe
9 hours ago






@Stefan: I hear you, but there are exactly two classes for which it is semantically meaningful to subclass B, and I'm trying to express/enforce this logical constraint in C++.

– Violet Giraffe
9 hours ago













2 Answers
2






active

oldest

votes


















21














This is a new feature added to C++17. What is going on is C is now considered an aggregate. Since it is an aggregate, it doesn't need a constructor. If we look at [dcl.init.aggr]/1 we get that an aggregate is




An aggregate is an array or a class with



  • no user-provided, explicit, or inherited constructors ([class.ctor]),


  • no private or protected non-static data members (Clause [class.access]),


  • no virtual functions, and


  • no virtual, private, or protected base classes ([class.mi]).


[ Note: Aggregate initialization does not allow accessing protected and private base class' members or constructors.  — end note ]




And we check of all those bullet points. You don't have any constructors declared in C or D so there is bullet 1. You don't have any data members so the second bullet doesn't matter, and your base class is public so the third bullet is satisfied.



The change that happened between C++11/14 and C++17 that allows this is that aggregates can now have base classes. You can see the old wording here where it expressly stated that bases classes are not allowed.



We can confirm this by checking the trait std::is_aggregate_v like



int main()

std::cout << std::is_aggregate_v<C>;



which will print 1.




Do note that since C is a friend of B you can use



C c;
C c1;
C c2 = C();


As valid ways to initialize a C. Since D is not a friend of B the only one that works is D d; as that is aggregate initialization. All of the other forms try to default initialize and that can't be done since D has a deleted default constructor.






share|improve this answer




















  • 1





    I guess writing defaulted constructor definition out of class like B::B() = default; will be considered a user-provided constructor, while defaulting it in class is considered a not-user-provided constructor?

    – VTT
    8 hours ago






  • 2





    @VTT That makes no difference. B() = default inside the class is still a user declared constructor.

    – NathanOliver
    8 hours ago






  • 1





    @NathanOliver you sure? I'm quite certain that during one of the many talks in cppcon one of the lecturers explained the difference, although it may be applied elsewhere, not in this very example. Can't find the link tho.

    – Fureeish
    8 hours ago






  • 1





    @Fureeish 100% sure. What moving the constructor out of the class does change is how the object is initialized: stackoverflow.com/questions/54350114/…

    – NathanOliver
    8 hours ago






  • 2





    @VioletGiraffe D d2(); is the most vexing parse so you have a function, not an object.

    – NathanOliver
    8 hours ago


















-4














From What is the default access of constructor in c++:



If there is no user-declared constructor for class X, a constructor having no parameters is implicitly declared as defaulted. An implicitly-declared default constructor is an inline public member of its class.



If the class definition does not explicitly declare a copy constructor, one is declared implicitly. [...] An implicitly-declared copy/move constructor is an inline public member of its class.



Constructors for classes C and D are generated internally by compiler.



BTW.: If you want to play with inheritance, please make sure you have virtual destructor defined.






share|improve this answer




















  • 1





    I think you misunderstood the point of my confusion, although your link is still relevant. I know each C and D has a default public constructor, but D is not supposed to be able to instantiate the instance of its base class B because of the latter's constructor being private.

    – Violet Giraffe
    9 hours ago











  • does this answer the question?

    – sp2danny
    9 hours ago











  • So why B() = default; is threated as "no user-declared constructor"?

    – VTT
    9 hours ago











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55535107%2fwhy-doesnt-a-class-having-private-constructor-prevent-inheriting-from-this-clas%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









21














This is a new feature added to C++17. What is going on is C is now considered an aggregate. Since it is an aggregate, it doesn't need a constructor. If we look at [dcl.init.aggr]/1 we get that an aggregate is




An aggregate is an array or a class with



  • no user-provided, explicit, or inherited constructors ([class.ctor]),


  • no private or protected non-static data members (Clause [class.access]),


  • no virtual functions, and


  • no virtual, private, or protected base classes ([class.mi]).


[ Note: Aggregate initialization does not allow accessing protected and private base class' members or constructors.  — end note ]




And we check of all those bullet points. You don't have any constructors declared in C or D so there is bullet 1. You don't have any data members so the second bullet doesn't matter, and your base class is public so the third bullet is satisfied.



The change that happened between C++11/14 and C++17 that allows this is that aggregates can now have base classes. You can see the old wording here where it expressly stated that bases classes are not allowed.



We can confirm this by checking the trait std::is_aggregate_v like



int main()

std::cout << std::is_aggregate_v<C>;



which will print 1.




Do note that since C is a friend of B you can use



C c;
C c1;
C c2 = C();


As valid ways to initialize a C. Since D is not a friend of B the only one that works is D d; as that is aggregate initialization. All of the other forms try to default initialize and that can't be done since D has a deleted default constructor.






share|improve this answer




















  • 1





    I guess writing defaulted constructor definition out of class like B::B() = default; will be considered a user-provided constructor, while defaulting it in class is considered a not-user-provided constructor?

    – VTT
    8 hours ago






  • 2





    @VTT That makes no difference. B() = default inside the class is still a user declared constructor.

    – NathanOliver
    8 hours ago






  • 1





    @NathanOliver you sure? I'm quite certain that during one of the many talks in cppcon one of the lecturers explained the difference, although it may be applied elsewhere, not in this very example. Can't find the link tho.

    – Fureeish
    8 hours ago






  • 1





    @Fureeish 100% sure. What moving the constructor out of the class does change is how the object is initialized: stackoverflow.com/questions/54350114/…

    – NathanOliver
    8 hours ago






  • 2





    @VioletGiraffe D d2(); is the most vexing parse so you have a function, not an object.

    – NathanOliver
    8 hours ago















21














This is a new feature added to C++17. What is going on is C is now considered an aggregate. Since it is an aggregate, it doesn't need a constructor. If we look at [dcl.init.aggr]/1 we get that an aggregate is




An aggregate is an array or a class with



  • no user-provided, explicit, or inherited constructors ([class.ctor]),


  • no private or protected non-static data members (Clause [class.access]),


  • no virtual functions, and


  • no virtual, private, or protected base classes ([class.mi]).


[ Note: Aggregate initialization does not allow accessing protected and private base class' members or constructors.  — end note ]




And we check of all those bullet points. You don't have any constructors declared in C or D so there is bullet 1. You don't have any data members so the second bullet doesn't matter, and your base class is public so the third bullet is satisfied.



The change that happened between C++11/14 and C++17 that allows this is that aggregates can now have base classes. You can see the old wording here where it expressly stated that bases classes are not allowed.



We can confirm this by checking the trait std::is_aggregate_v like



int main()

std::cout << std::is_aggregate_v<C>;



which will print 1.




Do note that since C is a friend of B you can use



C c;
C c1;
C c2 = C();


As valid ways to initialize a C. Since D is not a friend of B the only one that works is D d; as that is aggregate initialization. All of the other forms try to default initialize and that can't be done since D has a deleted default constructor.






share|improve this answer




















  • 1





    I guess writing defaulted constructor definition out of class like B::B() = default; will be considered a user-provided constructor, while defaulting it in class is considered a not-user-provided constructor?

    – VTT
    8 hours ago






  • 2





    @VTT That makes no difference. B() = default inside the class is still a user declared constructor.

    – NathanOliver
    8 hours ago






  • 1





    @NathanOliver you sure? I'm quite certain that during one of the many talks in cppcon one of the lecturers explained the difference, although it may be applied elsewhere, not in this very example. Can't find the link tho.

    – Fureeish
    8 hours ago






  • 1





    @Fureeish 100% sure. What moving the constructor out of the class does change is how the object is initialized: stackoverflow.com/questions/54350114/…

    – NathanOliver
    8 hours ago






  • 2





    @VioletGiraffe D d2(); is the most vexing parse so you have a function, not an object.

    – NathanOliver
    8 hours ago













21












21








21







This is a new feature added to C++17. What is going on is C is now considered an aggregate. Since it is an aggregate, it doesn't need a constructor. If we look at [dcl.init.aggr]/1 we get that an aggregate is




An aggregate is an array or a class with



  • no user-provided, explicit, or inherited constructors ([class.ctor]),


  • no private or protected non-static data members (Clause [class.access]),


  • no virtual functions, and


  • no virtual, private, or protected base classes ([class.mi]).


[ Note: Aggregate initialization does not allow accessing protected and private base class' members or constructors.  — end note ]




And we check of all those bullet points. You don't have any constructors declared in C or D so there is bullet 1. You don't have any data members so the second bullet doesn't matter, and your base class is public so the third bullet is satisfied.



The change that happened between C++11/14 and C++17 that allows this is that aggregates can now have base classes. You can see the old wording here where it expressly stated that bases classes are not allowed.



We can confirm this by checking the trait std::is_aggregate_v like



int main()

std::cout << std::is_aggregate_v<C>;



which will print 1.




Do note that since C is a friend of B you can use



C c;
C c1;
C c2 = C();


As valid ways to initialize a C. Since D is not a friend of B the only one that works is D d; as that is aggregate initialization. All of the other forms try to default initialize and that can't be done since D has a deleted default constructor.






share|improve this answer















This is a new feature added to C++17. What is going on is C is now considered an aggregate. Since it is an aggregate, it doesn't need a constructor. If we look at [dcl.init.aggr]/1 we get that an aggregate is




An aggregate is an array or a class with



  • no user-provided, explicit, or inherited constructors ([class.ctor]),


  • no private or protected non-static data members (Clause [class.access]),


  • no virtual functions, and


  • no virtual, private, or protected base classes ([class.mi]).


[ Note: Aggregate initialization does not allow accessing protected and private base class' members or constructors.  — end note ]




And we check of all those bullet points. You don't have any constructors declared in C or D so there is bullet 1. You don't have any data members so the second bullet doesn't matter, and your base class is public so the third bullet is satisfied.



The change that happened between C++11/14 and C++17 that allows this is that aggregates can now have base classes. You can see the old wording here where it expressly stated that bases classes are not allowed.



We can confirm this by checking the trait std::is_aggregate_v like



int main()

std::cout << std::is_aggregate_v<C>;



which will print 1.




Do note that since C is a friend of B you can use



C c;
C c1;
C c2 = C();


As valid ways to initialize a C. Since D is not a friend of B the only one that works is D d; as that is aggregate initialization. All of the other forms try to default initialize and that can't be done since D has a deleted default constructor.







share|improve this answer














share|improve this answer



share|improve this answer








edited 6 hours ago

























answered 9 hours ago









NathanOliverNathanOliver

98.2k16138216




98.2k16138216







  • 1





    I guess writing defaulted constructor definition out of class like B::B() = default; will be considered a user-provided constructor, while defaulting it in class is considered a not-user-provided constructor?

    – VTT
    8 hours ago






  • 2





    @VTT That makes no difference. B() = default inside the class is still a user declared constructor.

    – NathanOliver
    8 hours ago






  • 1





    @NathanOliver you sure? I'm quite certain that during one of the many talks in cppcon one of the lecturers explained the difference, although it may be applied elsewhere, not in this very example. Can't find the link tho.

    – Fureeish
    8 hours ago






  • 1





    @Fureeish 100% sure. What moving the constructor out of the class does change is how the object is initialized: stackoverflow.com/questions/54350114/…

    – NathanOliver
    8 hours ago






  • 2





    @VioletGiraffe D d2(); is the most vexing parse so you have a function, not an object.

    – NathanOliver
    8 hours ago












  • 1





    I guess writing defaulted constructor definition out of class like B::B() = default; will be considered a user-provided constructor, while defaulting it in class is considered a not-user-provided constructor?

    – VTT
    8 hours ago






  • 2





    @VTT That makes no difference. B() = default inside the class is still a user declared constructor.

    – NathanOliver
    8 hours ago






  • 1





    @NathanOliver you sure? I'm quite certain that during one of the many talks in cppcon one of the lecturers explained the difference, although it may be applied elsewhere, not in this very example. Can't find the link tho.

    – Fureeish
    8 hours ago






  • 1





    @Fureeish 100% sure. What moving the constructor out of the class does change is how the object is initialized: stackoverflow.com/questions/54350114/…

    – NathanOliver
    8 hours ago






  • 2





    @VioletGiraffe D d2(); is the most vexing parse so you have a function, not an object.

    – NathanOliver
    8 hours ago







1




1





I guess writing defaulted constructor definition out of class like B::B() = default; will be considered a user-provided constructor, while defaulting it in class is considered a not-user-provided constructor?

– VTT
8 hours ago





I guess writing defaulted constructor definition out of class like B::B() = default; will be considered a user-provided constructor, while defaulting it in class is considered a not-user-provided constructor?

– VTT
8 hours ago




2




2





@VTT That makes no difference. B() = default inside the class is still a user declared constructor.

– NathanOliver
8 hours ago





@VTT That makes no difference. B() = default inside the class is still a user declared constructor.

– NathanOliver
8 hours ago




1




1





@NathanOliver you sure? I'm quite certain that during one of the many talks in cppcon one of the lecturers explained the difference, although it may be applied elsewhere, not in this very example. Can't find the link tho.

– Fureeish
8 hours ago





@NathanOliver you sure? I'm quite certain that during one of the many talks in cppcon one of the lecturers explained the difference, although it may be applied elsewhere, not in this very example. Can't find the link tho.

– Fureeish
8 hours ago




1




1





@Fureeish 100% sure. What moving the constructor out of the class does change is how the object is initialized: stackoverflow.com/questions/54350114/…

– NathanOliver
8 hours ago





@Fureeish 100% sure. What moving the constructor out of the class does change is how the object is initialized: stackoverflow.com/questions/54350114/…

– NathanOliver
8 hours ago




2




2





@VioletGiraffe D d2(); is the most vexing parse so you have a function, not an object.

– NathanOliver
8 hours ago





@VioletGiraffe D d2(); is the most vexing parse so you have a function, not an object.

– NathanOliver
8 hours ago













-4














From What is the default access of constructor in c++:



If there is no user-declared constructor for class X, a constructor having no parameters is implicitly declared as defaulted. An implicitly-declared default constructor is an inline public member of its class.



If the class definition does not explicitly declare a copy constructor, one is declared implicitly. [...] An implicitly-declared copy/move constructor is an inline public member of its class.



Constructors for classes C and D are generated internally by compiler.



BTW.: If you want to play with inheritance, please make sure you have virtual destructor defined.






share|improve this answer




















  • 1





    I think you misunderstood the point of my confusion, although your link is still relevant. I know each C and D has a default public constructor, but D is not supposed to be able to instantiate the instance of its base class B because of the latter's constructor being private.

    – Violet Giraffe
    9 hours ago











  • does this answer the question?

    – sp2danny
    9 hours ago











  • So why B() = default; is threated as "no user-declared constructor"?

    – VTT
    9 hours ago















-4














From What is the default access of constructor in c++:



If there is no user-declared constructor for class X, a constructor having no parameters is implicitly declared as defaulted. An implicitly-declared default constructor is an inline public member of its class.



If the class definition does not explicitly declare a copy constructor, one is declared implicitly. [...] An implicitly-declared copy/move constructor is an inline public member of its class.



Constructors for classes C and D are generated internally by compiler.



BTW.: If you want to play with inheritance, please make sure you have virtual destructor defined.






share|improve this answer




















  • 1





    I think you misunderstood the point of my confusion, although your link is still relevant. I know each C and D has a default public constructor, but D is not supposed to be able to instantiate the instance of its base class B because of the latter's constructor being private.

    – Violet Giraffe
    9 hours ago











  • does this answer the question?

    – sp2danny
    9 hours ago











  • So why B() = default; is threated as "no user-declared constructor"?

    – VTT
    9 hours ago













-4












-4








-4







From What is the default access of constructor in c++:



If there is no user-declared constructor for class X, a constructor having no parameters is implicitly declared as defaulted. An implicitly-declared default constructor is an inline public member of its class.



If the class definition does not explicitly declare a copy constructor, one is declared implicitly. [...] An implicitly-declared copy/move constructor is an inline public member of its class.



Constructors for classes C and D are generated internally by compiler.



BTW.: If you want to play with inheritance, please make sure you have virtual destructor defined.






share|improve this answer















From What is the default access of constructor in c++:



If there is no user-declared constructor for class X, a constructor having no parameters is implicitly declared as defaulted. An implicitly-declared default constructor is an inline public member of its class.



If the class definition does not explicitly declare a copy constructor, one is declared implicitly. [...] An implicitly-declared copy/move constructor is an inline public member of its class.



Constructors for classes C and D are generated internally by compiler.



BTW.: If you want to play with inheritance, please make sure you have virtual destructor defined.







share|improve this answer














share|improve this answer



share|improve this answer








edited 9 hours ago









TrebledJ

3,60521228




3,60521228










answered 9 hours ago









DiodacusDiodacus

1966




1966







  • 1





    I think you misunderstood the point of my confusion, although your link is still relevant. I know each C and D has a default public constructor, but D is not supposed to be able to instantiate the instance of its base class B because of the latter's constructor being private.

    – Violet Giraffe
    9 hours ago











  • does this answer the question?

    – sp2danny
    9 hours ago











  • So why B() = default; is threated as "no user-declared constructor"?

    – VTT
    9 hours ago












  • 1





    I think you misunderstood the point of my confusion, although your link is still relevant. I know each C and D has a default public constructor, but D is not supposed to be able to instantiate the instance of its base class B because of the latter's constructor being private.

    – Violet Giraffe
    9 hours ago











  • does this answer the question?

    – sp2danny
    9 hours ago











  • So why B() = default; is threated as "no user-declared constructor"?

    – VTT
    9 hours ago







1




1





I think you misunderstood the point of my confusion, although your link is still relevant. I know each C and D has a default public constructor, but D is not supposed to be able to instantiate the instance of its base class B because of the latter's constructor being private.

– Violet Giraffe
9 hours ago





I think you misunderstood the point of my confusion, although your link is still relevant. I know each C and D has a default public constructor, but D is not supposed to be able to instantiate the instance of its base class B because of the latter's constructor being private.

– Violet Giraffe
9 hours ago













does this answer the question?

– sp2danny
9 hours ago





does this answer the question?

– sp2danny
9 hours ago













So why B() = default; is threated as "no user-declared constructor"?

– VTT
9 hours ago





So why B() = default; is threated as "no user-declared constructor"?

– VTT
9 hours ago

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55535107%2fwhy-doesnt-a-class-having-private-constructor-prevent-inheriting-from-this-clas%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Category:Fedor von Bock Media in category "Fedor von Bock"Navigation menuUpload mediaISNI: 0000 0000 5511 3417VIAF ID: 24712551GND ID: 119294796Library of Congress authority ID: n96068363BnF ID: 12534305fSUDOC authorities ID: 034604189Open Library ID: OL338253ANKCR AUT ID: jn19990000869National Library of Israel ID: 000514068National Thesaurus for Author Names ID: 341574317ReasonatorScholiaStatistics

Reverse int within the 32-bit signed integer range: [−2^31, 2^31 − 1]Combining two 32-bit integers into one 64-bit integerDetermine if an int is within rangeLossy packing 32 bit integer to 16 bitComputing the square root of a 64-bit integerKeeping integer addition within boundsSafe multiplication of two 64-bit signed integersLeetcode 10: Regular Expression MatchingSigned integer-to-ascii x86_64 assembler macroReverse the digits of an Integer“Add two numbers given in reverse order from a linked list”

Kiel Indholdsfortegnelse Historie | Transport og færgeforbindelser | Sejlsport og anden sport | Kultur | Kendte personer fra Kiel | Noter | Litteratur | Eksterne henvisninger | Navigationsmenuwww.kiel.de54°19′31″N 10°8′26″Ø / 54.32528°N 10.14056°Ø / 54.32528; 10.14056Oberbürgermeister Dr. Ulf Kämpferwww.statistik-nord.deDen danske Stats StatistikKiels hjemmesiderrrWorldCat312794080n790547494030481-4