Why doesn't a class having private constructor prevent inheriting from this class? How to control which classes can inherit from a certain base? The 2019 Stack Overflow Developer Survey Results Are InC++ 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?

Time travel alters history but people keep saying nothing's changed

How to support a colleague who finds meetings extremely tiring?

Landlord wants to switch my lease to a "Land contract" to "get back at the city"

What could be the right powersource for 15 seconds lifespan disposable giant chainsaw?

Why do UK politicians seemingly ignore opinion polls on Brexit?

Right tool to dig six foot holes?

How come people say “Would of”?

Loose spokes after only a few rides

Is "plugging out" electronic devices an American expression?

Is an up-to-date browser secure on an out-of-date OS?

What is the accessibility of a package's `Private` context variables?

Which Sci-Fi work first showed weapon of galactic-scale mass destruction?

What is the motivation for a law requiring 2 parties to consent for recording a conversation

Did Scotland spend $250,000 for the slogan "Welcome to Scotland"?

slides for 30min~1hr skype tenure track application interview

What tool would a Roman-age civilization have for the breaking of silver and other metals into dust?

Can one be advised by a professor who is very far away?

Why do we hear so much about the Trump administration deciding to impose and then remove tariffs?

How to check whether the reindex working or not in Magento?

One word riddle: Vowel in the middle

Why is the Constellation's nose gear so long?

Worn-tile Scrabble

Resizing object distorts it (Illustrator CC 2018)

Did Section 31 appear in Star Trek: The Next Generation?



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



The 2019 Stack Overflow Developer Survey Results Are InC++ 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
    Apr 5 at 12:14











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

    – Violet Giraffe
    Apr 5 at 12:17






  • 2





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

    – vahancho
    Apr 5 at 12:17






  • 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
    Apr 5 at 12:24







  • 1





    I have never seen "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++." actually survive prolonged contact with the future. Some future programmer, perhaps even you, will curse you for failing to see their patently obvious need for another subclass.

    – Eric Towers
    Apr 5 at 17:26

















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
    Apr 5 at 12:14











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

    – Violet Giraffe
    Apr 5 at 12:17






  • 2





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

    – vahancho
    Apr 5 at 12:17






  • 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
    Apr 5 at 12:24







  • 1





    I have never seen "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++." actually survive prolonged contact with the future. Some future programmer, perhaps even you, will curse you for failing to see their patently obvious need for another subclass.

    – Eric Towers
    Apr 5 at 17:26













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 Apr 5 at 12:29







Violet Giraffe

















asked Apr 5 at 12:12









Violet GiraffeViolet Giraffe

15.1k29139256




15.1k29139256












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

    – Diodacus
    Apr 5 at 12:14











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

    – Violet Giraffe
    Apr 5 at 12:17






  • 2





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

    – vahancho
    Apr 5 at 12:17






  • 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
    Apr 5 at 12:24







  • 1





    I have never seen "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++." actually survive prolonged contact with the future. Some future programmer, perhaps even you, will curse you for failing to see their patently obvious need for another subclass.

    – Eric Towers
    Apr 5 at 17:26

















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

    – Diodacus
    Apr 5 at 12:14











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

    – Violet Giraffe
    Apr 5 at 12:17






  • 2





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

    – vahancho
    Apr 5 at 12:17






  • 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
    Apr 5 at 12:24







  • 1





    I have never seen "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++." actually survive prolonged contact with the future. Some future programmer, perhaps even you, will curse you for failing to see their patently obvious need for another subclass.

    – Eric Towers
    Apr 5 at 17:26
















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

– Diodacus
Apr 5 at 12:14





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

– Diodacus
Apr 5 at 12:14













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

– Violet Giraffe
Apr 5 at 12:17





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

– Violet Giraffe
Apr 5 at 12:17




2




2





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

– vahancho
Apr 5 at 12:17





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

– vahancho
Apr 5 at 12:17




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
Apr 5 at 12:24






@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
Apr 5 at 12:24





1




1





I have never seen "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++." actually survive prolonged contact with the future. Some future programmer, perhaps even you, will curse you for failing to see their patently obvious need for another subclass.

– Eric Towers
Apr 5 at 17:26





I have never seen "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++." actually survive prolonged contact with the future. Some future programmer, perhaps even you, will curse you for failing to see their patently obvious need for another subclass.

– Eric Towers
Apr 5 at 17:26












2 Answers
2






active

oldest

votes


















24














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
    Apr 5 at 12:44






  • 2





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

    – NathanOliver
    Apr 5 at 12:45






  • 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
    Apr 5 at 12:46






  • 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
    Apr 5 at 12:49






  • 2





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

    – NathanOliver
    Apr 5 at 13:15


















-5














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
    Apr 5 at 12:19











  • does this answer the question?

    – sp2danny
    Apr 5 at 12:21











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

    – VTT
    Apr 5 at 12:23











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









24














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
    Apr 5 at 12:44






  • 2





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

    – NathanOliver
    Apr 5 at 12:45






  • 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
    Apr 5 at 12:46






  • 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
    Apr 5 at 12:49






  • 2





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

    – NathanOliver
    Apr 5 at 13:15















24














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
    Apr 5 at 12:44






  • 2





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

    – NathanOliver
    Apr 5 at 12:45






  • 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
    Apr 5 at 12:46






  • 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
    Apr 5 at 12:49






  • 2





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

    – NathanOliver
    Apr 5 at 13:15













24












24








24







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 Apr 5 at 14:50

























answered Apr 5 at 12:40









NathanOliverNathanOliver

98.6k16138218




98.6k16138218







  • 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
    Apr 5 at 12:44






  • 2





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

    – NathanOliver
    Apr 5 at 12:45






  • 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
    Apr 5 at 12:46






  • 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
    Apr 5 at 12:49






  • 2





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

    – NathanOliver
    Apr 5 at 13:15












  • 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
    Apr 5 at 12:44






  • 2





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

    – NathanOliver
    Apr 5 at 12:45






  • 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
    Apr 5 at 12:46






  • 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
    Apr 5 at 12:49






  • 2





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

    – NathanOliver
    Apr 5 at 13:15







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
Apr 5 at 12:44





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
Apr 5 at 12:44




2




2





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

– NathanOliver
Apr 5 at 12:45





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

– NathanOliver
Apr 5 at 12:45




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
Apr 5 at 12:46





@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
Apr 5 at 12:46




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
Apr 5 at 12:49





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

– NathanOliver
Apr 5 at 12:49




2




2





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

– NathanOliver
Apr 5 at 13:15





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

– NathanOliver
Apr 5 at 13:15













-5














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
    Apr 5 at 12:19











  • does this answer the question?

    – sp2danny
    Apr 5 at 12:21











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

    – VTT
    Apr 5 at 12:23















-5














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
    Apr 5 at 12:19











  • does this answer the question?

    – sp2danny
    Apr 5 at 12:21











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

    – VTT
    Apr 5 at 12:23













-5












-5








-5







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 Apr 5 at 12:27









TrebledJ

3,65421328




3,65421328










answered Apr 5 at 12:16









DiodacusDiodacus

2276




2276







  • 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
    Apr 5 at 12:19











  • does this answer the question?

    – sp2danny
    Apr 5 at 12:21











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

    – VTT
    Apr 5 at 12:23












  • 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
    Apr 5 at 12:19











  • does this answer the question?

    – sp2danny
    Apr 5 at 12:21











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

    – VTT
    Apr 5 at 12:23







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
Apr 5 at 12:19





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
Apr 5 at 12:19













does this answer the question?

– sp2danny
Apr 5 at 12:21





does this answer the question?

– sp2danny
Apr 5 at 12:21













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

– VTT
Apr 5 at 12:23





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

– VTT
Apr 5 at 12:23

















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

រឿង រ៉ូមេអូ និង ហ្ស៊ុយលីយេ សង្ខេបរឿង តួអង្គ បញ្ជីណែនាំ

Crop image to path created in TikZ? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)Crop an inserted image?TikZ pictures does not appear in posterImage behind and beyond crop marks?Tikz picture as large as possible on A4 PageTransparency vs image compression dilemmaHow to crop background from image automatically?Image does not cropTikzexternal capturing crop marks when externalizing pgfplots?How to include image path that contains a dollar signCrop image with left size given

Romeo and Juliet ContentsCharactersSynopsisSourcesDate and textThemes and motifsCriticism and interpretationLegacyScene by sceneSee alsoNotes and referencesSourcesExternal linksNavigation menu"Consumer Price Index (estimate) 1800–"10.2307/28710160037-3222287101610.1093/res/II.5.31910.2307/45967845967810.2307/2869925286992510.1525/jams.1982.35.3.03a00050"Dada Masilo: South African dancer who breaks the rules"10.1093/res/os-XV.57.1610.2307/28680942868094"Sweet Sorrow: Mann-Korman's Romeo and Juliet Closes Sept. 5 at MN's Ordway"the original10.2307/45957745957710.1017/CCOL0521570476.009"Ram Leela box office collections hit massive Rs 100 crore, pulverises prediction"Archived"Broadway Revival of Romeo and Juliet, Starring Orlando Bloom and Condola Rashad, Will Close Dec. 8"Archived10.1075/jhp.7.1.04hon"Wherefore art thou, Romeo? To make us laugh at Navy Pier"the original10.1093/gmo/9781561592630.article.O006772"Ram-leela Review Roundup: Critics Hail Film as Best Adaptation of Romeo and Juliet"Archived10.2307/31946310047-77293194631"Romeo and Juliet get Twitter treatment""Juliet's Nurse by Lois Leveen""Romeo and Juliet: Orlando Bloom's Broadway Debut Released in Theaters for Valentine's Day"Archived"Romeo and Juliet Has No Balcony"10.1093/gmo/9781561592630.article.O00778110.2307/2867423286742310.1076/enst.82.2.115.959510.1080/00138380601042675"A plague o' both your houses: error in GCSE exam paper forces apology""Juliet of the Five O'Clock Shadow, and Other Wonders"10.2307/33912430027-4321339124310.2307/28487440038-7134284874410.2307/29123140149-661129123144728341M"Weekender Guide: Shakespeare on The Drive""balcony"UK public library membership"romeo"UK public library membership10.1017/CCOL9780521844291"Post-Zionist Critique on Israel and the Palestinians Part III: Popular Culture"10.2307/25379071533-86140377-919X2537907"Capulets and Montagues: UK exam board admit mixing names up in Romeo and Juliet paper"Istoria Novellamente Ritrovata di Due Nobili Amanti2027/mdp.390150822329610820-750X"GCSE exam error: Board accidentally rewrites Shakespeare"10.2307/29176390149-66112917639"Exam board apologises after error in English GCSE paper which confused characters in Shakespeare's Romeo and Juliet""From Mariotto and Ganozza to Romeo and Guilietta: Metamorphoses of a Renaissance Tale"10.2307/37323537323510.2307/2867455286745510.2307/28678912867891"10 Questions for Taylor Swift"10.2307/28680922868092"Haymarket Theatre""The Zeffirelli Way: Revealing Talk by Florentine Director""Michael Smuin: 1938-2007 / Prolific dance director had showy career"The Life and Art of Edwin BoothRomeo and JulietRomeo and JulietRomeo and JulietRomeo and JulietEasy Read Romeo and JulietRomeo and Julieteeecb12003684p(data)4099369-3n8211610759dbe00d-a9e2-41a3-b2c1-977dd692899302814385X313670221313670221