|
Thank you.
I also thought near to you.
class CMyClass{
public:
static int i;
CMyClass()
{
++i;
if(i>2)
{
printf("cannot create more than 2 instances");
~CMyClass();
}
}
~CMyClass()
{
--i;
}
};
void main()
{
CMyClass *pCls1 = new CMyClass();
CMyClass *pCls2 = new CMyClass();
CMyClass *pCls3 = new CMyClass();
}
But my friend sayingthat
Look up Singleton design pattern, since this is related to such a design pattern. Basically, you make the constructor private, and provide a public static member function to create an object. The static function controls whether to actually create a new object, or return one of the already created objects. Can I expect the solution in this way.
I dont know what is design patterns? Please help me.
Nice talking to you.
|
|
|
|
|
just know i have modified it.Check it
|
|
|
|
|
Just look at my other solution if you want something build on the singleton design pattern. It is the same pattern except that it allows creation of @ instances instead of just one
|
|
|
|
|
There is some problems with this implementation: first, what will happen if you do this:
CMyClass class1;<br />
CMyClass class2;<br />
CMyClass class3;
Then you will have a little problem .
And, what will return the new when you call the destructor inside the constructor ? I don't think it is a good idea to destruct an object that is not completely constructed, this could lead to some big troubles (pointers not initialized, ...).
|
|
|
|
|
sunit5 wrote: ForumVisual C++
Subject:Re: Can I restrict a class not to have more than 2 objects?
Sender:sunit5
Date:2:52 7 Dec '05
static CMyClass ::Counter=0;
initilaize a static member to zero;
// class constructor
CMyClass()
{
CMyClass ::counter++;
if(CMyClass ::Counter>2)
{
// call the destructor
this->~CMyClass()
return;
}
Hello Sir, AFAIK is know Explicit call to the Destructor is not good programming Practice.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
|
|
|
|
|
sunit5 wrote: CMyClass()
{
CMyClass ::counter++;
if(CMyClass ::Counter>2)
{
// call the destructor
this->~CMyClass()
return;
}
AFAIK, Whatever Goes.. The Object is still created.. but in your case destructor will call two times... here a dummy source code that will demonstrate that :-
#include<iostream.h>
class Classa
{
public:
Classa()
{
cout<<"in const"<<endl;
this->~Classa();
}
void print()
{
cout<<"abc"<<endl;
}
~Classa()
{
cout<<"In Dest"<<endl;
}
};
void main()
{
Classa a;
a.print();
}
calling Destructor doesn't mean the object is destroyed.. it is for doing cleanup tasks at end
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
|
|
|
|
|
Here is another approach, a little bit more 'protected' than the one from sunit.
First, put your constructor and destructor private. This will prevent to create an instance of the class when it's not allowed. So for example you won't be able to do that:
CMyClass MyClass;
As the constructor is protected, you will have a compile error.
Then, to be able to get an instance of the class, declare a static member function (it must be static so it can be called without having an instance of the object). You need to have also a static counter that counts the number of instances (it must be static as it must be shared among all instances):
class CMyClass<br />
{<br />
public:<br />
static CMyClass* GetNewInstance();<br />
<br />
protected:<br />
CMyClass();<br />
~CMyClass();<br />
<br />
private:<br />
static int m_iCounter;<br />
}
int CMyClass::m_iCounter = 0;<br />
CMyClass* CMyClass::GetNewInstance()<br />
{<br />
if (m_iCounter<2)<br />
{<br />
m_iCounter++;<br />
return new CMyClass;<br />
}<br />
return NULL;<br />
}
Now, the last thing to add is to be able to free the memory (and then decrement the counter when one of the object is freed). Add a public Destroy method:
void CMyClass::Destroy()<br />
{<br />
m_iCounter--;<br />
delete this;<br />
}
So, when you need a new instance of the object, call GetNewInstance:
CMyClass* NewClass = CMyClass::GetNewInstance();
It will return the new instance if succesfull or NULL if 2 instances already exists. Then, don't forget to free your object when not needed anymore:
NewClass->Destroy();
-- modified at 3:20 Wednesday 7th December, 2005
Sorry, I forgot to put my GetNewInstance function static
|
|
|
|
|
Hello,
It kinda scares me that the 'calling destructor inside constructor' solution was brought foward before this one! It's just scary to see that some people even think about doing that...
Behind every great black man...
... is the police. - Conspiracy brother
Blog[^]
|
|
|
|
|
nobody's perfect and we have to get them back on the right way
TOXCCT >>> GEII power [toxcct][VisualCalc 2.20] | soon : [VisualCalc 3.0]
|
|
|
|
|
You are really great. This is the reason I like you & your simplisity.
Nice talking to you.
|
|
|
|
|
Would you actually want the instantiation of the third object to fail, or would it suffice to make the object's methods unavailable if more than two exist?
"Take only what you need and leave the land as you found it." - Native American Proverb
|
|
|
|
|
Try the following:
1. Create a windows form application in Visual Studio (2003)
2. Set form background image to a picture. (mine is a 24 kb PNG file)
3. Create a textbox in the form. (AutoSize = false, Multiline = true)
4. Set form Opacity to < 100% (I set it to 50%)
Now run the program and type in the textbox. Just before the text "overflows", instead of smoothly auto scrolling the text down, my screen freezes for a good 20 secs. After doing this for a couple of times I get a ialmrnt5 display driver fail error and my Windows XP crashes and restarts.
Has this happened to anyone else? Is there a way around this? Thanks.
|
|
|
|
|
Hi,
How do I retrieve data in 5th parameter of this API. Since I dont know what will be the length of data n how will I allocate memory to this param???
Even if I declare array of maximum size it gives me error "234" i.e. "ERROR_MORE_DATA"
Here is sample code...
DWORD dwretrievedDataType = NULL;
DWORD dwretrievedLenOfData = NULL;
LPWSTR wzretrievedData = NULL;
.
.
.
// I have already given proper values for 1st 3 params
dwretValue = SHGetValue(hkeyRoot, wzkeyPath, wzvalueOfKey,
&dwretrievedDataType, wzretrievedData, &dwretrievedLenOfData);
.
.
.
PLz do let me know asap if anyone knows abt it...
thanks n regards
Supriya Tonape
|
|
|
|
|
Heelo.
The last (sixth) parameter is supposed to be the allocated length of the buffer.
You must set dwretrievedLenOfData to the size of wzretrievedData before the call to SHGetValue.
Now you set it to NULL.
BTW, if you want to zero out a DWORD, the use 0 instead of NULL.
Kakan
|
|
|
|
|
Hi Kakan,
I dont want to hard code any value, like size of 5th param... b4 giving this param as input to 'SHGetValue()' U need to allocate memory for 'dwretrievedData'..I did read abt the documentation of it in MSDN
Thanks in ton for help coz I am done with my problem
bye
Supriya Tonape
|
|
|
|
|
Hello again.
I'm pleased to be able to help you out.
About the buffer size, at some time or another you will have to allocate the buffer for the return value, hardcoded or not...
In general, the Windows functions that return buffers usually has a way to give the coder the size of buffer needed for a particular reply.:
1.
Set the buffer pointer to NULL, and allocated buffer size to 0.
2.
Call the function in question.
3.
The function returns the number of bytes/chars needed for the (real) response.
4.
Allocate that amount of memory för the buffer.
5.
Call the function again, this time with a valid buffer address and the number of allocated bytes.
6.
Obtain the result.
But I'm not sure if this goes for SHGetValue, though. I haven't checked.
But this principle goes for many Windows functions.
Kakan.
|
|
|
|
|
Hi,
No It doesnt work for it. Though it doesnt give any error but buffer size returned is the size of DWORD.
Bye
Supriya Tonape
|
|
|
|
|
I am creating a MS Word document using Automation With VC++(MFC). I want to change the Font Size of the text in the MS Word Document by using Automation with VC++ (MFC).
Thanks
|
|
|
|
|
Hi i posted a Query " How can i run my code in Service mode "
I got quick reply ;
"Kakan" guided me and "ThatsAlok" given link
Thanku guys , four kind help...
But actually my problem is when i click on my project ".exe" file
then it should automatically installed , rather then going to
Comand promt and installing manually....
Thanks in advance.....
Usha
|
|
|
|
|
usha_dolly wrote: But actually my problem is when i click on my project ".exe" file
then it should automatically installed , rather then going to
CreateService Api will help... But beware.. Always check that service existence before installing...
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
|
|
|
|
|
I have made an application which can convert PPT slides to JPG images using powerpoint type library.
Now i want to port the application to MAC. I m not able to find any developing environment in which i can port it to mac. I have a MAC G4 iBook and Office 2004 for MAC .. Please help me...
my mail address is catbbs7@yahoo.com
Regards
Vishal
|
|
|
|
|
:-OHi all,
Please tell whether we can use
ShellExecute(NULL,"print",m_FileName,NULL,NULL,SW_HIDE);
to print
1) Text(*.txt) file
2) Document file
3) Excel Sheet
4) Access table
5) Powerpoint Slide
6) Webpages
7) Images Jpg,jpeg,bmp etc.
I am asking this because i used automation to achieve printing for most of these. As an afterthought, it came perhaps i could have done it using ShellExecute also.Am i right in thinking so?i don't know. u know....
then plz tell because if that happens it will make my application really simple & fast without any overhead.
Please Guide.
Regards,
Ankush Mehta
|
|
|
|
|
Ankush Mehta wrote: 1) Text(*.txt) file
2) Document file
3) Excel Sheet
4) Access table
5) Powerpoint Slide
6) Webpages
7) Images Jpg,jpeg,bmp etc.
Try it, if it doenst work then you will need to find another method.
1: Should work
2: Should work
3:Should work
4: Nope, will need to find another method, I would dump it to an excel file and then print that.
5: Nope wont work, have no idea never really worked with power point.
6: Should work.
7: Dont know.
ZeePain! wrote: This seems like one of those programs that started small, grew incrementally, building internal pressure, and finally barfed all over its source code sneakers. Or something.
thedailywtf.com[^]
|
|
|
|
|
thanks sfdougl,
1) Agree
2) Agree
3) Agree
4) Agree,automation is the option in my case because i want to enumerate tables in the database programmatically & then proceed towards printing.i think i am right.plz tell if u have other option to enlist tables in database.
5) it doesn't work in my case
6)I could not manage printing a webpage with ShellExecute.My code is like this:
ShellExecute(NULL,"print",m_FileName,NULL,NULL,SW_HIDE);
7)Prints only bmp.I changed extension to bmp, printed & renamed filename to original extension
Please tell me about point 6) after trying it out.Also tell me about point 4) if you have something to tell about it.
Regards,
Ankush Mehta
|
|
|
|
|
Ankush Mehta wrote: 4) if you have something to tell about it.
Here is a good quick way to dump a recordset to a excel file, then call shellexecute to print that file. Delete it it after your done.
CString str;
str.Format("SELECT * INTO [Excel 8.0;DATABASE=%s].[%s] FROM [%s]", ExcelFile, Worksheet, table);
Create an ADO Connection object to the database then call.
pConn->Execute(str.AllocSysString(), NULL, adExecuteNoRecords);
To enumerate the tables in an access database try
SELECT MSysObjects.Type, MSysObjects.Name
FROM MSysObjects
WHERE MSysObjects.Type=1;
Ankush Mehta wrote: 6)I could not manage printing a webpage with ShellExecute.My code is like this:
ShellExecute(NULL,"print",m_FileName,NULL,NULL,SW_HIDE);
Try ShellExecute("explorer.exe","print",m_FileName,NULL,NULL,SW_HIDE);
ZeePain! wrote: This seems like one of those programs that started small, grew incrementally, building internal pressure, and finally barfed all over its source code sneakers. Or something.
thedailywtf.com[^]
|
|
|
|