Can I stop people by deriving my class?
I mean I don't want my class to be as a base class...
Can I do that? 12 1623
Shraddha wrote:
Can I stop people by deriving my class?
I mean I don't want my class to be as a base class...
Can I do that?
Yes. See the FAQ.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
On Jun 19, 11:10 am, Shraddha <shraddhajosh.. .@gmail.comwrot e:
Can I stop people by deriving my class?
I mean I don't want my class to be as a base class...
Can I do that?
Yes you can but you have to do some tweaks with your class's
construction and with inheritance.
class Usable;
class Usable_lock {
friend class Usable;
private:
Usable_lock() {}
Usable_lock(con st Usable_lock&) {}
};
class Usable : public virtual Usable_lock {
// ...
public:
Usable();
Usable(char*);
// ...
};
Usable a;
class DD : public Usable { };
DD dd; // error: DD::DD() cannot access
// Usable_lock::Us able_lock(): private member
You can check more about it at http://www.research.att.com/~bs/bs_f...#no-derivation
Regards,
Sarath http://sarathc.wordpress.com/
"Shraddha" <sh************ *@gmail.comwrot e in message
news:11******** *************@o 11g2000prd.goog legroups.com...
Can I stop people by deriving my class?
I mean I don't want my class to be as a base class...
Can I do that?
3 ways, shown in the FAQ: http://www.parashift.com/c++-faq-lit...html#faq-23.11
Shraddha wrote:
Can I stop people by deriving my class?
I mean I don't want my class to be as a base class...
Can I do that?
Technically, yes. See: http://www.research.att.com/~bs/bs_f...#no-derivation
A slightly different approach uses a protected constructor:
/*
| This defines the macro
|
| FINAL
|
| to be used to prevent derivation:
|
| struct X : FINAL {};
|
| struct Y : X {}; // declaring variables of this type won't work
*/
// credits:
/*
Found in news.lang.c++.m oderated (Gennaro Prota)
see: http://groups.google.com/group/comp....3980680a2f7136
*/
class protected_const ructor {
protected:
protected_const ructor ( void ) {}
}; // protected_const ructor
#define FINAL private virtual protected_const ructor
struct X : FINAL {};
struct Y : X {}; //
int main ( void ) {
X x; // fine.
Y y; // compile time error.
}
_However_ (!!!), as explained in Stroustrups FAQ, it is highly doubtfull
that you have a valid reason to do something nasty like this. Keep in mind
that you are probably unable to predict the reasons that clients of your
code have to derive from your classes. Note that C++ supports multiple
programming styles, where different reasons for derivation are considered
valid. I doubt that you have sufficient information to decide for the
client code programmers that derivation is wrong. That is their
responsibility. Trust them; and if they screw up, it's their problem. That
said, there might be cases where you have a valid reason, although I don't
know of any convincing examples.
Best
Kai-Uwe Bux
On Jun 19, 4:10 am, Shraddha <shraddhajosh.. .@gmail.comwrot e:
Can I stop people by deriving my class?
I mean I don't want my class to be as a base class...
Can I do that?
Yes and no. Unless you're in a very strange situation, just
documenting the fact that the class isn't designed to be used as
a base class should be enough. In fact, unless you're in a very
strange situation, just not documenting how to use it as a base
class should be sufficient.
If it's not, you've got a serious problem, no matter what you
do.
--
James Kanze (GABI Software, from CAI) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
On Tue, 19 Jun 2007 09:06:07 -0000, James Kanze wrote:
>On Jun 19, 4:10 am, Shraddha <shraddhajosh.. .@gmail.comwrot e:
>Can I stop people by deriving my class? I mean I don't want my class to be as a base class... Can I do that?
Yes and no. Unless you're in a very strange situation, just documenting the fact that the class isn't designed to be used as a base class should be enough. In fact, unless you're in a very strange situation, just not documenting how to use it as a base class should be sufficient.
If it's not, you've got a serious problem, no matter what you do.
BTW, to clarify a too common misunderstandin g, one may render any
derived class non-instantiable, not preventing derivation itself (or
usage of accessible static members in the derived class).
class final
{
protected:
final();
};
#define FINAL_CLASS private virtual final
// usage:
class my_class : FINAL_CLASS
{};
Why one might want to do that is another story :-)
--
Gennaro Prota -- Need C++ expertise? I'm available https://sourceforge.net/projects/breeze/
(replace 'address' with 'name.surname' to mail)
"James Kanze" <ja*********@gm ail.comwrote in message
news:11******** ************@u2 g2000hsc.google groups.com...
On Jun 19, 4:10 am, Shraddha <shraddhajosh.. .@gmail.comwrot e:
Can I stop people by deriving my class?
I mean I don't want my class to be as a base class...
Can I do that?
"Yes and no. Unless you're in a very strange situation, just
documenting the fact that the class isn't designed to be used as
a base class should be enough. "
A non-virtual destructor would be telltale "documentation" .
John
On Jun 22, 7:57 am, "JohnQ" <johnqREMOVETHI Sprogram...@yah oo.com>
wrote:
"James Kanze" <james.ka...@gm ail.comwrote in message
news:11******** ************@u2 g2000hsc.google groups.com...
On Jun 19, 4:10 am, Shraddha <shraddhajosh.. .@gmail.comwrot e:
Can I stop people by deriving my class?
I mean I don't want my class to be as a base class...
Can I do that?
"Yes and no. Unless you're in a very strange situation, just
documenting the fact that the class isn't designed to be used as
a base class should be enough. "
A non-virtual destructor would be telltale "documentation" .
Not always. What about std::exception?
In fact, most of the time, I think the opposite applies. Unless
the class is documented explicitly to be a base class, you
shouldn't derive from it.
--
James Kanze (GABI Software, from CAI) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
"James Kanze" <ja*********@gm ail.comwrote in message
news:11******** **************@ p77g2000hsh.goo glegroups.com.. .
On Jun 22, 7:57 am, "JohnQ" <johnqREMOVETHI Sprogram...@yah oo.com>
wrote:
"James Kanze" <james.ka...@gm ail.comwrote in message
news:11******** ************@u2 g2000hsc.google groups.com...
On Jun 19, 4:10 am, Shraddha <shraddhajosh.. .@gmail.comwrot e:
Can I stop people by deriving my class?
I mean I don't want my class to be as a base class...
Can I do that?
"Yes and no. Unless you're in a very strange situation, just
documenting the fact that the class isn't designed to be used as
a base class should be enough. "
A non-virtual destructor would be telltale "documentation" .
"Not always. What about std::exception? "
It's destructor is declared virtual in my implementation.
"In fact, most of the time, I think the opposite applies. Unless
the class is documented explicitly to be a base class, you
shouldn't derive from it."
John This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Hans De Winter |
last post by:
Hi,
I am really puzzled why my compiler does not accept the following code:-
--- CODE STARTS HERE ---
#include <iostream>
using namespace std;
class Foo
{
|
by: Steven T. Hatton |
last post by:
This may be another question having an obvious answer, but I'm not seeing
it. I'm trying to create a class that derives from
std::valarray<std::string>. I don't need a template, and I haven't come
across any examples of a construct like what I show below. Perhapes it's
simply a bad idea. If there is something fundamentally wrong with this
approach please let me know.
Can anybody tell me if there is a way to get the following to work? I...
|
by: JustSomeGuy |
last post by:
I need to write an new class derived from the list class.
This class stores data in the list to the disk if an object
that is added to the list is over 1K in size.
What methods of the std stl list class must Ioverride in order for this
to work?
|
by: Julian |
last post by:
I would like to have output from my program to be written to cout as well as
a file. (actually, i want several other output options but this should
explain my problem in the simplest way). I have seen commercial programs
print output to the screen as well as to a log file.
depending on the user and other situations, i might want to turn off one of
the outputs or maybe even both outputs.
so, i want a single line with operator << function...
|
by: Mahesh Devjibhai Dhola |
last post by:
Hi All,
I want to make a custom class in c#, which extends System.Xml.XmlNode class
of BCL.
Now in custom class, I have implement abstract methods of XmlNode class
also. Now when I am trying to run the class it gives an error that
"System.Xml.XmlNode.XmlNode() is inaccessible due to its protection level".
This error comes because XmlNode has not any public constructor. I found
XmlNode has two constructor but both are private or friend...
| |
by: Nindi73 |
last post by:
HI
If I define the class DoubleMap such that
struct DoubleMap : public std::map<std::string, double>{};
Is there any overhead in calling std::map member functions ?
Moreover are STL containers destructors virtual >
|
by: Mosfet |
last post by:
Hi,
I would like more info about deriving from an existing C struct.
Let's say I am fed up with always writing the same code shown below :
MYSTRUCT foo;
memset( &foo, 0, sizeof(MYSTRUCT) );
foo.cbSize = sizeof(MYSTRUCT);
|
by: Christopher Pisz |
last post by:
I set out to make a custom logger. Examining some other code laying around,
I came across one that derived from ostream, and had a associated class
derived from streambuf. Is this practice a good idea? I was under the
impression that deriving from std classes that do not have virtual methods
was bad. Is ostream designed to be derived from?
If, so are there any resources on how to do this properly? ostream has alot
if internals that look...
|
by: Al Grant |
last post by:
Consider two translation units, (1):
namespace { struct S { }; }
struct D: S { virtual int f(void); };
and (2):
#include <typeinfo>
struct D;
char const *f(D *p) { return typeid(p).name(); }
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |