473,544 Members | 1,149 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Private constructor

1) Is there any use of defining a class with a single constructor
declared in private scope? I am not asking a about private copy
constructors to always force pass/return by reference.

2) Is this in any way used to create singletons. Can someone say how?

Cheers,
Andy
Jul 22 '05 #1
34 3058
Andy wrote:
1) Is there any use of defining a class with a single constructor
declared in private scope? I am not asking a about private copy
constructors to always force pass/return by reference.

2) Is this in any way used to create singletons. Can someone say how?


If you want to create singletons, you have to declare all your constructors
(and operator=, too) as private.

Then you create a public function that checks if singleton was created
(through some static bool member) and then calls (private) constructor.

Then (if check passed) your function returns refernece (or pointer) on your
singleton and set true flag to static member -- singleton was created.

--
mP

http://pivoluska.matfyz.cz/
Jul 22 '05 #2

"Andy" <ga***********@ yahoo.com> wrote in message
news:96******** *************** ***@posting.goo gle.com...
1) Is there any use of defining a class with a single constructor
declared in private scope? I am not asking a about private copy
constructors to always force pass/return by reference.

2) Is this in any way used to create singletons. Can someone say how?


The point of making a constructor private is usually to NOT allow anyone to
use it. (Imagine that instead of the "private" keyword it was
"inaccessible". ) That allows you to have a class available for use, but not
for anyone to just create one with a plain constructor. Normally you'd
provide some other means for creating one, where you can control whether it
gets created or not. For example, you could have a static function called
"create", and that would have to be called to get a new object. In the
create function, you can first check to see if you've created one before, by
keeping a flag or counter. If so, you don't create one. If not, you create
one and return one. That way no more than one can ever exist.
Jul 22 '05 #3
"jeffc" <no****@nowhere .com> wrote in message news:<3f******* *@news1.prserv. net>...
"Andy" <ga***********@ yahoo.com> wrote in message
news:96******** *************** ***@posting.goo gle.com...
1) Is there any use of defining a class with a single constructor
declared in private scope? I am not asking a about private copy
constructors to always force pass/return by reference.

2) Is this in any way used to create singletons. Can someone say how?


The point of making a constructor private is usually to NOT allow anyone to
use it. (Imagine that instead of the "private" keyword it was
"inaccessible". ) That allows you to have a class available for use, but not
for anyone to just create one with a plain constructor. Normally you'd
provide some other means for creating one, where you can control whether it
gets created or not. For example, you could have a static function called
"create", and that would have to be called to get a new object. In the
create function, you can first check to see if you've created one before, by
keeping a flag or counter. If so, you don't create one. If not, you create
one and return one. That way no more than one can ever exist.


Just one question - even the static creator function needs to create
an instance of the class on the heap or stack. Without a constructor
how can that be made possible. Or is it that everything is static and
the singleton is stateless? So we never need an instance. I think I am
missing something.
Jul 22 '05 #4
Andy wrote:

"jeffc" <no****@nowhere .com> wrote in message news:<3f******* *@news1.prserv. net>...
"Andy" <ga***********@ yahoo.com> wrote in message
news:96******** *************** ***@posting.goo gle.com...
1) Is there any use of defining a class with a single constructor
declared in private scope? I am not asking a about private copy
constructors to always force pass/return by reference.

2) Is this in any way used to create singletons. Can someone say how?


The point of making a constructor private is usually to NOT allow anyone to
use it. (Imagine that instead of the "private" keyword it was
"inaccessible". ) That allows you to have a class available for use, but not
for anyone to just create one with a plain constructor. Normally you'd
provide some other means for creating one, where you can control whether it
gets created or not. For example, you could have a static function called
"create", and that would have to be called to get a new object. In the
create function, you can first check to see if you've created one before, by
keeping a flag or counter. If so, you don't create one. If not, you create
one and return one. That way no more than one can ever exist.


Just one question - even the static creator function needs to create
an instance of the class on the heap or stack. Without a constructor
how can that be made possible. Or is it that everything is static and
the singleton is stateless? So we never need an instance. I think I am
missing something.


Detail!
He didn't say that the class dosn't have a constructor (which by the
way is impossible). He said that the constructor is private! And
like an other private class member it can be used from a class member
function only. Since the static creator function is a member function,
it can use the constructor.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #5
Karl Heinz Buchegger wrote:

Andy wrote:

"jeffc" <no****@nowhere .com> wrote in message news:<3f******* *@news1.prserv. net>...
"Andy" <ga***********@ yahoo.com> wrote in message
news:96******** *************** ***@posting.goo gle.com...
> 1) Is there any use of defining a class with a single constructor
> declared in private scope? I am not asking a about private copy
> constructors to always force pass/return by reference.
>
> 2) Is this in any way used to create singletons. Can someone say how?

The point of making a constructor private is usually to NOT allow anyone to
use it. (Imagine that instead of the "private" keyword it was
"inaccessible". ) That allows you to have a class available for use, but not
for anyone to just create one with a plain constructor. Normally you'd
provide some other means for creating one, where you can control whether it
gets created or not. For example, you could have a static function called
"create", and that would have to be called to get a new object. In the
create function, you can first check to see if you've created one before, by
keeping a flag or counter. If so, you don't create one. If not, you create
one and return one. That way no more than one can ever exist.
Just one question - even the static creator function needs to create
an instance of the class on the heap or stack. Without a constructor
how can that be made possible. Or is it that everything is static and
the singleton is stateless? So we never need an instance. I think I am
missing something.


Detail!
He didn't say that the class dosn't have a constructor (which by the
way is impossible).


Sorry. POD's indeed don't have a constructor.
He said that the constructor is private! And
like an other private class member it can be used from a class member
function only. Since the static creator function is a member function,
it can use the constructor.


--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #6

"Andy" <ga***********@ yahoo.com> wrote in message
news:96******** *************** ***@posting.goo gle.com...

The point of making a constructor private is usually to NOT allow anyone to use it. (Imagine that instead of the "private" keyword it was
"inaccessible". ) ....


Just one question - even the static creator function needs to create
an instance of the class on the heap or stack. Without a constructor
how can that be made possible. Or is it that everything is static and
the singleton is stateless? So we never need an instance. I think I am
missing something.


What I wrote was a little misleading. When I said "NOT allow anyone to use
it", I didn't mean literally anyone. I mean anyone *else* outside the
class. The class itself can access its own private constructor. See Karl's
answer. e.g.

class A
{
private:
A() {}
public:
static A* createAnA();
};

A* A::createAnA()
{
return new A;
}

int main()
{
A* pA = A::createAnA();
}
Jul 22 '05 #7
"jeffc" <no****@nowhere .com> wrote in message news:<3f******* *@news1.prserv. net>...
"Andy" <ga***********@ yahoo.com> wrote in message
news:96******** *************** ***@posting.goo gle.com...

The point of making a constructor private is usually to NOT allow anyone to use it. (Imagine that instead of the "private" keyword it was
"inaccessible". ) ....
Just one question - even the static creator function needs to create
an instance of the class on the heap or stack. Without a constructor
how can that be made possible. Or is it that everything is static and
the singleton is stateless? So we never need an instance. I think I am
missing something.


What I wrote was a little misleading. When I said "NOT allow anyone to use
it", I didn't mean literally anyone. I mean anyone *else* outside the
class. The class itself can access its own private constructor. See Karl's
answer. e.g.


I am sorry I made a stupid mistake. I was under the impression that
just as static functions cannot access instance data members of a
class, they also cannot call non-static member functions. Of course
member functions are not per instance even if they are made to look
that way. Constructor being a special kind of such a function should
be accessible from static member functions.

class A
{
private:
A() {}
public:
static A* createAnA();
};

A* A::createAnA()
{
return new A;
}

int main()
{
A* pA = A::createAnA();
}


I was just wondering, though this is purely a design issue, what
happens to the pointer that we get from the CreateAnA creator
function. The client code will need to explicitly call "delete" on
this pointer -- not a good thing. We would perhaps need a manager
class in between which takes care of calling delete in its destructor
.... or may be something like an auto_ptr type smart and cocky pointer
pretenders.

The singleton
--------------

The code snippet that you gave does not serve a singleton though. How
do we do that. I tried doing it this way:

----->

#include <iostream>

class CPrivCons
{
private:
CPrivCons(int n) : m_nInt(n)
{
s_cRefCnt=0;
s_This = NULL;
}

int m_nInt;
static int s_cRefCnt;
static CPrivCons *s_This;

public:
static CPrivCons& CreateObj(int n)
{
if(s_cRefCnt==0 ){
std::cout<<"New InstanceCreated "<<std::end l;

s_This = new CPrivCons(n);
}

s_cRefCnt++;

return *s_This;
}

static void DestroyObj()
{
if(--s_cRefCnt == 0){
std::cout<<"Obj ectDestroyed"<< std::endl;

delete s_This;
}
}

int get_IntVal()
{
int n = m_nInt;

return n;
}

void set_IntVal(int n)
{
m_nInt = n;
}
};

int CPrivCons::s_cR efCnt = 0;
CPrivCons *CPrivCons::s_T his = NULL;
int main()
{
CPrivCons& p = CPrivCons::Crea teObj(10);

CPrivCons& p1 = CPrivCons::Crea teObj(12);

std::cout<<p.ge t_IntVal()<<std ::endl;

std::cout<<p1.g et_IntVal()<<st d::endl;;

p1.set_IntVal(1 9);

std::cout<<p.ge t_IntVal()<<std ::endl;

CPrivCons::Dest royObj();
CPrivCons::Dest royObj();

return 0;
}
<-----
The above code can of course not be used in a multi-threaded
situation. Besides, I feel there should be a separate manager
interface interposed between the singleton and the client.

Cheers,
Andy
Jul 22 '05 #8
<
I am sorry I made a stupid mistake. I was under the impression that
just as static functions cannot access instance data members of a
class, they also cannot call non-static member functions. Of course
member functions are not per instance even if they are made to look
that way. Constructor being a special kind of such a function should
be accessible from static member functions.


That was stupider on second thoughts. A constructor does not get a
this pointer secretly. So it's not a special kind of the others ... it
is special and different from the rest. Nevertheless, I think I am
right in assuming that neither the member functions nor the
constructor or destructor constitute the state of an instantiated
object - therefore static member functions should be able to access
them.
Jul 22 '05 #9

"Andy" <ga***********@ yahoo.com> wrote in message
news:96******** *************** ***@posting.goo gle.com...
<
I am sorry I made a stupid mistake. I was under the impression that
just as static functions cannot access instance data members of a
class, they also cannot call non-static member functions. Of course
member functions are not per instance even if they are made to look
that way. Constructor being a special kind of such a function should
be accessible from static member functions.


That was stupider on second thoughts. A constructor does not get a
this pointer secretly. So it's not a special kind of the others ... it
is special and different from the rest. Nevertheless, I think I am
right in assuming that neither the member functions nor the
constructor or destructor constitute the state of an instantiated
object - therefore static member functions should be able to access
them


I have no clue what you are talking about. Constructors are non-static
member functions. You can't call them. They do have this pointers.
You're confusing access with instantiation. A static member function
has access to the private members of other objects of the same class
just like non-static members of one object can access private
members of other instances of the class.
Jul 22 '05 #10

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

Similar topics

12
2636
by: Will | last post by:
Is it possible to have private instance members in a javascript class? function myObject() { var private = 1; this.getPrivate = function() { return private; } this.incrementPrivate = function() {
10
2467
by: Ioannis Vranos | last post by:
May someone explain why does this compile? class HiddenSealBaseClass { public: HiddenSealBaseClass() { } }; class Sealed: virtual HiddenSealBaseClass
4
6111
by: baumann | last post by:
hi all, according the private / protected access control, - private; that is, its name can be used only by members and friends of the class in which it is declared. - protected; that is, its name can be used only by members and friends of the class in which it is
1
1833
by: Andrew Poulos | last post by:
I having some trouble understanding how to make functions private I have created an instance of an object using a constructor function and there are 4 prototypes: Comm = function() { //blah } Comm.prototype.get = function() {
7
2701
by: Jan | last post by:
Hi There, I want to insert an object with private copy constructor at the and of a STL list. Is there an easy way to do it like that: list <x> myList; // x has private copy constructor .... public void y::insert(x& obj) {
4
5510
by: plmanikandan | last post by:
Is static constructor available in C++? Can anybody explain me about private and static constructors in c++ Rgds, Mani
3
3377
by: John Salmon | last post by:
g++ complains about illegal access to a private member when the following is compiled with a private copy constructor for the class C. When the copy constructor is public, the program runs and demonstrates(?) that the copy constructor is never called (at least no sign of its chatter on std::cout is visible). So - is it necessary to have a...
10
3434
by: siddhu | last post by:
Dear Experts, I want to make a class whose objects can be created only on heap. I used the following approach. class ss { ss(){} public: void* operator new(size_t sz)
12
2542
by: Premal | last post by:
Hi, I tried to make delete operator private for my class. Strangely it is giving me error if I compile that code in VC++.NET. But it compiles successfully on VC++6.o. Can anybody give me inputs about it. I wanted that on my class delete should not work. Object pointer should be deleted using my function only which is taking care of...
0
7777
jinu1996
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...
0
5916
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...
1
5301
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...
0
4922
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...
0
3420
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...
0
3414
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1844
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
1
990
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
671
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...

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.