|
pix_programmer wrote: why the constructor of a singleton class is declared as private?
because out of the class, object could not be instantiated
pix_programmer wrote: What would happen if it is not declared as private?
If you leave its constructor as public, the class won't be singleton anymore!
Navigate the Given link to know about singleton in Details
Singleton pattern
[^]
Hope It will works for you.
|
|
|
|
|
To forbid anybody to create an instance of the class. The idea of a singleton (as its name suggest) is that only one instance of the class exist. If you have a public constructor, it means that anybody can create instances of the class. When the constructor is declared public, if you try to instantiate the class outside of itself, the code will generate an error.
So, if you make the constructor public, nothing will "happen" directly but your design is flawed: you are not sure anymore that your class can be instantiated multiple times.
|
|
|
|
|
As others have pointed out, the idea is that the implementation of a singleton class has full control over the property of ever having only one instance. So you need to make sure that there is no way to create a second instance, if one is already in existance, meaning:
1. You can not create an instance with a constructor without going through the class's code
2. You can not copy an (i. e. the only existing) instance to make another one
3. You can access the one existing instance or create it from anywhere
The first is achieved by not providing a constructor that is public. The problem however is, that the C/C++ compiler implicitely always creates a default constructor if there is no other, and that default constructor would be dclared as public! Therefore we mus explicitely declare a default constructor that is private to prevent the compiler from creating a public one.
The second would be achieved by not creating a copy constructor, but again, the compiler implicitely creates one if there is no other. The safest way to prevent this is again, to explicitely define a private copy constructor.
The third can be achieved by defining a static function that delivers a reference or pointer to the one instance in existance. This function can check whether there already is an instance and retrun that, or if there is none, use the private constructor to create it.
|
|
|
|
|
Very good explaination! Deserves a 5!
|
|
|
|
|
Hi all
How can i write Special character in CSV File(like ,)?
Please help me
|
|
|
|
|
Hi,
You may use the separator parameter of a std::ostream_iterator , for instance:
#include <iostream>
#include <iterator>
int main()
{
int vals[] = {1, 2, 3 ,4, 5};
std::copy_n(vals, 5, std::ostream_iterator<int>(std::cout, ","));
}
cheers,
AR
When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)
|
|
|
|
|
Thanks for reply
I use like this
CStdioFile test;
CString itemtext="ABC,123";
CString itemtextm="1";
if (test.Open"C:\\Test.csv"CFile::modeCreate | CFile::modeWrite | CFile::modeNoTruncate))
{
test.Seek(0,CFile::end);
test.WriteString("Name");
test.WriteString(",");
test.WriteString("Class");
test.WriteString("\n");
.WriteString(itemtext);
test.WriteString(",");
test.WriteString(itemtextm);
test.WriteString("\n");
test.Close();
}
Now problem is that itemtext comes with two column.I want to Write this(itemtext)in Single Column. Please help me
|
|
|
|
|
With your code the output should be Name,Class\nABC,123,1\n. What output do you want with the same input?
When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)
|
|
|
|
|
As far as I understood, he wants the string "ABC,123" be considered as one column in the CSV file. But this has nothing to do with C++, rather with the CSV syntax in which he has put double quotes around the field.
|
|
|
|
|
I am not very good at guess games
When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)
|
|
|
|
|
Well, spending a lot of time in the programming forums improves that a lot...
|
|
|
|
|
This has nothing to do with C++ but rather to the CSV format. If you google for the format specification (see here[^] for instance) you can see that:
Fields with embedded commas must be enclosed within double-quote characters.
|
|
|
|
|
|
How is the correct way to subclassing my CHeaderCtrl class into my CListCtrl ?
Let say I have CListCtrlEx , ( public of CListCtrl ) , and I have a CHeaderCtrlEx class ( public of CHeaderCtrl ) and I need to subclassing into CListCtrlEx ... how it's correct way to do it ?
I tell you for what I need subclassing : I want to improve my CListCtrlEx class to CHeaderCtrlEx class from here[^] and saw there , that CHeaderCtrlEx is subclassing into CListCtrlEx ...
|
|
|
|
|
For a dialog it could be performed as following :
BOOL CYourDialog::OnInitDialog()
{
BOOL bResult = CDialog::OnInitDialog();
CHeaderCtrl* pcOriginalHeader(m_cListCtrl.GetHeaderCtrl());
if (pcOriginalHeader->GetSafeHwnd()) {
m_cHeader.SubclassWindow(pcOriginalHeader->GetSafeHwnd());
}
return bResult;
}
They sought it with thimbles, they sought it with care;
They pursued it with forks and hope;
They threatened its life with a railway-share;
They charmed it with smiles and soap.
|
|
|
|
|
And what if I create the list control dinamicaly ( with Create ) ?
int CTestList3View::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if(CListView::OnCreate(lpCreateStruct) == -1)return -1;
DWORD dwStyle = WS_CHILD | WS_VISIBLE | WS_TABSTOP | LVS_REPORT;
BOOL bResult = m_List1.Create(dwStyle,CRect(0,0,0,0),this,IDC_LIST1);
return (bResult ? 0 : -1);
}
|
|
|
|
|
Then - place the subclassing lines previos to the 'return' line...
They sought it with thimbles, they sought it with care;
They pursued it with forks and hope;
They threatened its life with a railway-share;
They charmed it with smiles and soap.
|
|
|
|
|
It goes with PreSubclassWindow() right after Create() !
Thanks for solution ! Thanks all of you !
|
|
|
|
|
|
I use App Wizard to create a new MFC ActiveX Control. On the "Control Settings" page, I select SysListView32 for "Create control based on:". But now, I don't know how to add new column and item to the list view. I try to get the handle by using GetSafeHwnd() and then send LVM_INSERTCOLUMN message but nothing happens.
modified on Thursday, December 16, 2010 4:09 AM
|
|
|
|
|
Assuming the handle is correct, this approach seems okay. Maybe the LVCOLUMN wasn't filled correctly?
Try something like this:
LVCOLUMN col;
col.mask = LVCF_TEXT|LVCF_WIDTH|LVCF_FMT;
col.pszText = "Name";
col.cx = 200;
col.fmt = LVCFMT_LEFT;
SendMessage(hwndList,LVM_INSERTCOLUMN,0,(int)&col);
modified 13-Sep-18 21:01pm.
|
|
|
|
|
Thanks, I tried your code but nothing displayed. Can anybody show me step by step to add new columns and items? I use VS2005!
|
|
|
|
|
--------------------------------------------------------------------------------
Hi ALL
I created a new dialog resouce. Now I want to remove it from the project. How to do this.
Thanks
|
|
|
|
|
Just select that dialog (IID_DIALOG1)in Resource view and press the delete button. Thats it....
|
|
|
|
|
Hi,
I have to Store CMap Object with "Key Name" as CString and "Key Value"
as CArray Object.is it possible?
|
|
|
|