473,769 Members | 3,383 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can we have private constructors and destructors

Can we have private constructors and destructors? IF yes what is the
use of such constructors or destructors.... .in the sense where can
these be implemented in a system......... ........

I have an idea that we can have private constructors and destructors
but am not able to find a situation where they can be used...

Regards
RVG
ra********@opus soft.com
Jul 19 '05 #1
3 21391
Rajesh Garg wrote:
Can we have private constructors and destructors? IF yes what is the
use of such constructors or destructors.... .in the sense where can
these be implemented in a system......... ........

I have an idea that we can have private constructors and destructors
but am not able to find a situation where they can be used...


Typically you declare methods private to prevent others from calling
them. If you declare a constructor private, you prevent others from
creating objects of your class from the outside. This makes sense under
some circumstances, e.g. if you implement the so called singleton
pattern. This pattern makes sure, that only one object of a certain
class exists, so you have to control explicitly, who creates that one
and only object.

Cheers,

<maik/>

Jul 19 '05 #2
Rajesh Garg <ra*******@redi ffmail.com> wrote in message
news:14******** *************** ***@posting.goo gle.com...
Can we have private constructors and destructors?
Yes, in fact in a class they are private by default if you don't specify the
access rights.
IF yes what is the
use of such constructors or destructors.... .in the sense where can
these be implemented in a system......... ........


You might use them if there is allowed to be only one instance of a class
(called a singleton). You allow a single function to access the constructor
(either a friend or a public static member function), and it will only
create an instance if it hasn't already done so. You don't want just anyone
to be able to delete that one instance, so you could make the destructor
private as well.

DW

Jul 19 '05 #3
"Tamer Higazi" <ta**********@w eb.de> wrote...
As long as i know are constructors and destructors only provided in an
PUBLIC Area to give parameters for a class.

All constructors and destructors MUST be declarated "public" to get
access to it.
Nonsense. Just like other members of the class, constructors and
destructors can have access specifiers which limit the places where
objects of that class can be created or destroyed. "Private" access
specifier limits those places to either other member functions of
the same class or friends (classes or functions).
The private area are class variables and methods which can be called
from public methods only.
No. They can be just as well called from private member functions
or from functions declared as friends or from member functions of
classes declared as friends.
But you won't be able to call these methods
and variables outside the class in the "main" part.
Yes, that's the main idea.
Konstruktors and Destruktors are only good if you want to give special
values on the way to a class. Even you can overload them, by having
different numbers (kinds) of parameters.
Nonsense. Constructors are needed if you want to have the control
over the process of constructing of the object (opening files,
checking system conditions, etc.)
the destructor destroys the konstructor and frees the memorie.
This statement doesn't make sense.
you are NOT forced to create an Konstruktor and Destruktor. If you don't
declare them, then a Standard Kon- and destruc- tor will be called
Not "standard". "Default".
I make you an example:

class GoodTest
{

private:
int result;

public:

GoodTest(int a, int b, int c)
{
result = a * b *c;
}

~GoodTest()
{
};

void output
{
cout << "result is: " << result;
}

};

int main()
{

GoodTest Teste(3,4,5);
Teste.output();
return 0;
}
Along with the fact that this example doesn't compile due to errors,
it also doesn't demonstrate the use of _private_ constructors or
destructors as the OP asked.

Rajesh Garg wrote:
Can we have private constructors and destructors? IF yes what is the
use of such constructors or destructors.... .in the sense where can
these be implemented in a system......... ........

I have an idea that we can have private constructors and destructors
but am not able to find a situation where they can be used...


Polymorphic objects. Constructed by a "factory method" or a friend
function, the objects reside in freestore. The user receives
a pointer to the object and uses it, then passes it to be destroyed
in another function.

class SomeBaseClass {
SomeBaseClass() ; // private constructor
virtual ~SomeBaseClass( ); // private destructor
public:
static SomeBaseClass* createMeOne(int ); // loaded from a DLL
virtual void doWork();
static void deleteThis(Some BaseClass*&);
};

int main() {
SomeBaseClass* p = SomeBaseClass:: createMeOne(42) ;
p->doWork();
SomeBaseClass:: deleteThis(p);
}

... somewhere in the library

class SomeDerivedClas s : public SomeBaseClass {
public:
SomeDerivedClas s(int);
private:
void doWork() {
doActualWork();
}
};

SomeBaseClass* SomeBaseClass:: createMeOne(int what) {
return new SomeDerivedClas s(what); /// <<<<<<<<<<<<<<< << !!!
}

void SomeBaseClass:: deleteThis(Some BaseClass*& ptr) {
delete ptr;
ptr = 0;
}

Victor
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2956
by: Takeshi | last post by:
I have code as ff: typedef double* (*DBLPTRFUNCPTR)(int) ; typedef int* (*INTPTRFUNCPTR)(int) ; typedef double (*DBLFUNCPTR)(int) ; typedef int (*INTFUNCPTR)(int) ; etc .... typedef struct
0
1130
by: KK | last post by:
I'd really like to use XML serialization for a data file that's important to my application. The data file will be used later at runtime to populate a PropertyGrid. The problem for me is that XML serialization doesn't work on objects without parameterless constructors. This is bad, because there are several important System.Drawing namespace objects I'd like to save, but they all have parameterless constructors. The most notable of these...
2
7467
by: Doug | last post by:
I am using reflection to read an assembly and I have a class that has only a private constructor. However when I use the following method. Type.GetConstructors(BindFlags.NonPublic); It does not pick up the private constructor. Instead it reads that there are no constructors. How do I use this method to determine there
12
2584
by: Joe Narissi | last post by:
I know how to create and use static constructors, but is there a such thing as a static destructor? If not, then how do you deallocate memory intialized in the static constructor? Thanks in advance, Joe
9
5804
by: Peter Oliphant | last post by:
I've been told that value structs can have default constructors (I'm using VS C++.NET 2005 PRO using clr:/pure syntax). But the following code generates the following error: value struct ValueStruct { double x ; double y ; ValueStruct() { x = y = double(0) ; } // error * } ;
12
8960
by: Preets | last post by:
Can anyone explain to me the exact use of private constructors in c++ ?
0
9423
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,...
0
10210
Oralloy
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...
1
9990
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,...
0
9861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8869
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7406
isladogs
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...
0
5298
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...
1
3956
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2814
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.