|
Why we use destructors???and what functionality they perform???
|
|
|
|
|
|
Destructor is a function that is automatically called (by the compiler) when an object on the stack goes out of scope or when delete is called on pointer pointing to an object of a class.
Destructors are normally used to perform cleanup on the object.
Destructors along with constructors form an important concept in C++ called Resource Acquisition Is Initialization or RAII - http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization[^]
This concept is used a lot in several C++ libraries like the STL and Boost.
|
|
|
|
|
Destructor - cleans memory occupied by Object.
|
|
|
|
|
No it doesnt.
It is a funciton called when an object goes out of scope and can do anything it likes.
|
|
|
|
|
Destructor is a funtion - cleans memory occupied by Object. when it goes out of scope.
|
|
|
|
|
Because they are automatically called when an object goes out of scope and saves you having to remember to call a kind of 'un_init' function on your object.
|
|
|
|
|
Hello,
I am trying to use CDHtmlDialog in a MFC dialog based project.
What i want to achieve is pretty sample. Have one image, one button and one text box in the HTML page.
And then dynamically change this based on a timer. i.e., there're multiple images in the folder and it will change every seconds. Once the button is clicked, the timer stops and no more image changes. Same time when image changes the text in the text box also should change.
I know this is pretty simple, but for some reason i am not able to make the image even display in the dialog.
I am using Visual Studio 2012 in Windows 7 machine.
Can somebody share some sample code that does this?
I appreciate your help.
Thanks in advance.
|
|
|
|
|
|
Perhaps #1 should update his old article[^].
Soren Madsen
"When you don't know what you're doing it's best to do it quickly" - Jase #DuckDynasty
|
|
|
|
|
Well spotted.
Veni, vidi, abiit domum
|
|
|
|
|
I have few specific questions
1. When i try to build the sample projects from web in the Visual Studio 2012, it gives the following error --> atlmfc\include\atlwinverapi.h(29): fatal error C1189: #error : This file requires _WIN32_WINNT to be #defined at least to 0x0403. Value 0x0501 or higher is recommended.
How to fix this?
2. In the project i created, i am trying to load an image into the DHTML page and that doesn't work. How to load the image into the DHTML page? Also how to change the image in a DHTML page from the MFC code?
2. How can i change the text in a DHTML page from the MFC code?
Thanks in advance.
|
|
|
|
|
Don Guy wrote: This file requires _WIN32_WINNT to be #defined at least to 0x0403. Value 0x0501 or higher is recommended.
How to fix this? Add a define at the beginning of your stdafx.h file.
Don Guy wrote: How to load the image into the DHTML page? Also how to change the image in a DHTML page from the MFC code? Check the documentation[^], or the article that Soren found.
3. same as 2. Use the features of MFC to set and get dialog data.
Veni, vidi, abiit domum
|
|
|
|
|
I am defining a vector as a private variable of a class Grid. Class Points has only two instance vars that are all ints. Did I initialize constructor correctly and When writing a destructor for Grid do I need to write a destructor for vector like this: ~vecotr() or with delete or using iterator?
class Grid{
public:
Grid(vector<Points> v) : vector<Points>(3, 0) {};
~Grid() {
~vector<Points>(); }
~Grid() {
delete v_points;
}
~Grid() {
for (vector<Points>::iterator it = v_points.begin(),
vector<Points>::iterator it_end = v_points.end(); it != it_end; it++)
}
private:
vector<Points> v_points;
};
Which option should I use and did I initialize constructor correctly?
|
|
|
|
|
|
You din't need any of your options. The vector destructor is automatically called by the Grid one. In turn the vector destructor calls the destructor of every object stored.
Try the following code:
#include <iostream>
#include <vector>
using namespace std;
class A
{
public:
~A(){cout << "A dtor" << endl;}
};
class B
{
vector <A> v;
public:
B()
{
A a;
v.push_back(a);
v.push_back(a);
v.push_back(a);
}
};
int main()
{
B b;
}
Veni, vidi, vici.
modified 30-Aug-13 4:24am.
|
|
|
|
|
The vector initialization is not correct. It should be
Grid(vector<points> v) : v_points(v) {};
Also you should consider making the parameter a const reference. This way you avoid one copy operation.
The good thing about pessimism is, that you are always either right or pleasently surprised.
|
|
|
|
|
which initialization is better considering I will definitely have to increase the number of points at runtime? Will option 3 stop me form doing that?
option 1:
private:
vector<Points> const v_points
option 2:
private:
const vector<Points> v_points
option 3:
private:
const vector<Points> const v_points
|
|
|
|
|
None will work for your purpose: the first and second are equivalent, and, if it is syntactically valid at all, so is the third.
The previous comment about a const reference refers to your constructor code, not the member variable declaration. I. e. you should write
Grid(const vector<Points>& v) : v_points(v) {};
instead of
Grid(vector<Points> v) : vector<Points>(3, 0) {};
This will initialize the member variable v_points with the exact same values that are stored in the vector v you pass as a fcuntion parameter.
If you want a constructor that initializes your vector with 3 points, you don't need the parameter v, so you only need:
Grid() : v_points(3) {};
Note that these three points will be initialized by calling the default constructor for your class Point. If that constructor does not initialize it the way you want, you must change the values inside your constructor body like this:
Grid() : v_points(3) {
for (auto it_point = v_points.begin(); it_point != v_points.end(); ++it_point)
*it_point = 0; };
|
|
|
|
|
How to pass array from MFC (dialogbased) to win 32 DLL ?
In MFC application I have read csv file and stored 2nd column into into array(Elastmod)how do I pass that array to win 32 DLL?right now it only takes first element of array not all the eelements.
// MFC File
extern "C" __declspec(dllimport)void CommonBinFrame(char Scanfile[_MAX_PATH],float E[25]);
// read .csv file
CString Token;
CString Separator = _T(",");
int Position = 0;
CString strFilePath = ("C:\\dm1.csv");
CString strText;
CStdioFile readFile;
CFileException fileException;
if (readFile.Open(strFilePath, CFile::modeRead, &fileException))
{
while(readFile.ReadString(strText))
{
float Elastmod[25];
float *P;
P= Elastmod;
int Position = 0;
while(strText.Tokenize(Separator, Position)!="")
{
for(int i=0;i<=24;i++)
{
int Position = 0;
Token = strText.Tokenize(Separator, Position);
Token = strText.Tokenize(Separator, Position);
Elastmod[i] = atof(Token);
readFile.ReadString(strText);
}
}
char Scanfile[_MAX_PATH];
CommonBinFrame(Scanfile,Elastmod); // Here I got full array which i wanted to pass to DLL
}
}
else
{
CString strErrorMsg;
strErrorMsg.Format(_T("Can't open file %s , error : %u"),"C:\\dm1.csv" , fileException.m_cause);
AfxMessageBox(strErrorMsg);
}
readFile.Close();
}
//DLL file
extern "C" __declspec(dllexport)void CommonBinFrame(char Scanfile, float *E);
void CommonBinFrame(char Scanfile, float E[])
{
}
Float E[]= only have 1st array element
Thank you.
|
|
|
|
|
Since Elastmod points to the array, when you pass Elastmod , you ARE passing the whole array.
It looks to me as if the real problem is how do you tell the CommonBinFrame function the size of the array?
Do you have the source code for the DLL?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
//Dll Code
extern "C" __declspec(dllexport)void CommonBinFrame(char Scanfile, float Elstmod[25]);
float Poisson[] = {0.3,0.3,0.3,0.3,0.3,0.3,0.3,0.3,0.3,0.3,0.3,0.3,0.3,0.3,0.3,
char Scanfile[_MAX_PATH]; 0.3};
void CommonBinFrame(char Scanfile, float Elastmod[25])
{
//some code//
new_header_out(Elastmod);
//some code//
}
void new_header_out(float Elastmod[25)
{
//some code//
for(count=0;count
|
|
|
|
|
Well, if the CommonBinFrame function is expecting an array with 25 elements, then I guess all is well.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
peoria123 wrote:
extern "C" __declspec(dllexport)void CommonBinFrame(char Scanfile, float *E);
void CommonBinFrame(char Scanfile, float E[])
{
}
Float E[]= only have 1st array element
I'm not sure what that is supposed to be but it does not look like valid code. You also have a separate issue that your function definition declares its first parameter as a char rather than a char* . It should rather be something like:
extern "C" __declspec(dllexport)void CommonBinFrame(char* Scanfile, float *E);
void CommonBinFrame(char* Scanfile, float *E)
{
printf("%s\n", Scanfile);
for (int i = 0; i < 25; ++i)
{
printf("item[%d]: %6.2f\n", i, E[i]);
}
}
<div class="signature">Veni, vidi, abiit domum</div>
|
|
|
|
|
I have a hierarchy of classes processing serial (COM port) data.
The UI is CpropertyPage ( in main thread) and the port data is retrieved by a COM port thread.
It all works, sort of.
Two “problems” ,related.
I would like to be able to receive error messages from each stage and need to pass the data received back to the CpropertyPage.
I thought that I could use Post/Send messages to do this, but I cannot figure out how to pass the CpropertyPointer “down the line”.
Here is the first working attempt snippet:
In CpropertyPage class :
SendMessage(WM_SERIAL_OUTPUT); // message received in CpropertyPage OK
CString strTest = this->strFrequency; // just checking OK
lResult = pC_SerialMFC->pC_SerialEx->Open(strPort, 0,0,true,this ); // OK
Here is the first “down step” and it fails
LONG C_SerialEx::Open (LPCTSTR lpszDevice, DWORD dwInQueue,
DWORD dwOutQueue, bool fStartListener, void* pointer )
{
TRACE("\nOpen port %s using base class ",lpszDevice ); // Call the base class first
CString strTest = pointer.strFrequency; // compiler error – see below
SendMessage((HWND) pointer, WM_SERIAL_OUTPUT,0,0); // no message received - I did not use Spy to trace where it went
left of '.strFrequency' must have class/struct/union type
So – how do I pass CproperyPage “this” pointer down the line ? Or is this approach all wrong?
Any constructive help would be greatly appreciated.
Cheers Vaclav
|
|
|
|