Prev: Converting from int** to vector< vector<int> >&
Next: how to pass polymorphic container classes
From: GH on 18 May 2010 11:04 Is there any way at compile time to restrict direct inheritance from a class to no more than once? Thanks for any insight. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Nick Hounsome on 19 May 2010 01:30 On 19 May, 03:04, GH <yzg...(a)gmail.com> wrote: > Is there any way at compile time to restrict direct inheritance from a > class to no more than once? Thanks for any insight. No offence intended but in my experience when someone asks how to stop somebody doing something in C++ it is a design error 98% of the time. Of course this might be in that rare 2% in which case the reasons for wanting the restriction are probably more interesting than any way of enforcing it. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Goran on 19 May 2010 07:24 On May 19, 4:04 am, GH <yzg...(a)gmail.com> wrote: > Is there any way at compile time to restrict direct inheritance from a > class to no more than once? Thanks for any insight. Not that I know of. But I think that the problem with your question is that you want to do something else, and you think that restricting inheritance will help. Perhaps if you say what you really want to achieve, someone might offer a solution to your actual problem. In other words, don't go "I want to do X. To do X, I'll start with Y, because once I have Y, I can easily do X. Darn, how do I do Y?" Instead, when you want to do X, ask "how do I do X?" :-). Goran. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: TJorgenson on 19 May 2010 07:42 On May 18, 7:04 pm, GH <yzg...(a)gmail.com> wrote: > Is there any way at compile time to restrict direct inheritance from a > class to no more than once? Thanks for any insight. { quoted banner removed; please do it yourself. -mod } You could make your base class constructor private (default or otherwise) and make your one and only derived class a friend of the base. This assumes that the base class knows who the one and only derived class is which probably means that these 2 classes should be 1. I don't know of a way to do this without the base knowing the derived. But why would you want to do this? -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: analizer on 19 May 2010 07:41 On 19 май, 06:04, GH <yzg...(a)gmail.com> wrote: > Is there any way at compile time to restrict direct inheritance from a > class to no more than once? Thanks for any insight. probably this could help: class Guard { Guard(){} Guard(const Guard&){} // not sure if this is required friend class MyClass; friend class OnlyAllowedSuccessor; }; class MyClass : virtual Guard { // Some implementation of your class here }; class OnlyAllowedSuccessor : MyClass { // Some implementation of your class derived from MyClass }; -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
|
Next
|
Last
Pages: 1 2 Prev: Converting from int** to vector< vector<int> >& Next: how to pass polymorphic container classes |