|
let's say i have a class like this:
class a
{
private:
char name[10];
.
.
.
}
how do i give 'name' a value in the constructor?
chen.
|
|
|
|
|
name = {"10 chars long"};
OR
name = new char[10];
::strcpy(&name[0], "value"); // going from memory, args could be the wrong way around
If you use the second method, don't forget to release memory in the destructor. If you use the first, don't change the value of the string, which means it should be const. Overall, you'd do better to use a string class to hold the value, if practical.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
If you get the name from the outside, go with this:
<br />
class a( const char* a_name) {<br />
strcpy(name,a_name);
}<br />
However, if you know the name at compile time already, simply hard-code it - gives more speed and security.
<br />
class a {<br />
const char* get_name() const { return "blabla"; }<br />
}<br />
----------------------
~hamster1
|
|
|
|
|
Instead of using a char array, consider a string class:
class a
{
private:
std::string name;
.
.
.
};
And then a constructor may look like:
a::a(const char* yourName) : name(yourName)
{}
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
-- modified at 8:32 Tuesday 30th August, 2005
|
|
|
|
|
There is a possible memory leak
<pre>private: System::Void btnButton_Click(System::Object *sender, System::EventArgs *e)
{
frmTest *frm = new frmTest(); // Create new Form
frm->ShowDialog(this); // Show
}</pre>
and i have defined destructor of that form
<pre>~frmTest
{
MessageBox::Show(S"Destructor Called");
}</pre>
When i run this app, i click button many times, and no destructor called. when i close main form, destructor is called only once.
so i have add:
<pre>delete frm; // To be safe.</pre>
is this a memory leak?
thanks in advance.
|
|
|
|
|
No, frm is created on managed heap, which is controlled by GC. In general, with managed code, you should worry more about leaking non-memory resources.
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
|
|
|
|
|
Hai
In my applcation i want to list all machines in the network.
So i used the WNetOpenEnum API .
My code looks like,
[DllImport("Mpr.dll", EntryPoint="WNetOpenEnumA", CallingConvention=CallingConvention::Winapi)]
static ErrorCodes WNetOpenEnum(ResourceScope dwScope, ResourceType dwType, ResourceUsage dwUsage, NETRESOURCE* p, IntPtr &lphEnum);
NETRESOURCE* pRsrc = new NETRESOURCE(); //A managed class
IntPtr handle = IntPtr::Zero;
result = WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, RESOURCEUSAGE_ALL, pRsrc, handle);
But when i run the application got an error
" Can not marshal parameter #4: The type definition of this type has no layout information. "
Whats is the problem.
Plz help
|
|
|
|
|
Hello...
I believe you forget to define the StructLayoutAttribute for the paramater NETRESOURCE* p...
In general it'S Sequential...
|
|
|
|
|
Can somebodey point me to internet where can i find tutorial on multithreading. I prefear in MC++ but C# will do fine. I have found some tutorial, but i didn't see any sample, explenation on thread safe.
Thanks in advance
|
|
|
|
|
Hello,
I am trying to build a download accelarator (a program which downloads a file through several connections to increase the speed).
I have created four threads. Each thread downloads the file from diffrent offset and finallly I join al this parts.
The problem is that when I use one thread( without changing the thread's code) it much faster. It like the additional threads are slowing one another.
In each thread I initialize the connection (CInternetSession->CHttpSession) and later in the thread I have put a loop which downloads the files.
The initialization part is in a critical section.
Can anyone tell whats wrong. Did anyone encountered similar problem?
Sincerely yours
Y.R.
|
|
|
|
|
Hello All,
Does anybody know how to find out interfaces exposed by a driver (.sys file)?
Thanks in advance
|
|
|
|
|
I have App.exe which uses Dll.dll, I also add classical Microsoft manifest file App.manifest.exe to enable XP styles for common controls.
Now question how can I disable influence of manifest on Dll.dll
Which means I want that App.exe continue to use
comctl32.dll (ver 6) new one
but
Dll.dll should use
comctl32.dll (ver 5) old one
Load / Unload library - already try not helps
#define/#undefine IE_CONTLOLS - already try doesnt helps
I need to customize body of manifest somehow include or exclude styles in the dependency section
Thanks.
|
|
|
|
|
Hi,
I have created a test dll in Fortran that squares an input number.
Then I'm trying to call this dll from C++.NET using the following bit of code:
<br />
#include <iostream><br />
using namespace std;<br />
extern "C" __declspec(dllimport)double square(double X);<br />
<br />
int main(int argc, char* argv[])<br />
{<br />
double a=4;<br />
double asq = square(a);<br />
cout << a << " " << asq << endl;<br />
return 0;<br />
}<br />
<code><br />
<br />
I get the following error:<br />
HelloWorld error LNK2019: unresolved external symbol __imp__square referenced in function _main<br />
<br />
I've added MyDll.lib as a reference from the Fortran code but no success. (I'm not able to add MyDll.dll however, is this normal?)<br />
Any ideas please?<br />
Kash
|
|
|
|
|
In the C++ .NET Framework you don't import functions from a DLL like you do in Win32; the syntax is different. The correct method is to use the DllImport attribute, which is located in: System::Runtime::InteropServices.
Using the DllImport Attribute[^]
|
|
|
|
|
Hi all,
I've created a form with a number of child windows. One certain window is behaving very strangely. If I open the child window and then close it, the main form will not close - by this I mean I can't click the exit button or use the exit menu item. The main reason for my confusion is that this behaviour only happens on certain machines.
Does anyone have any knowledge of why this might be occurring? It must have something to do with the individual machines but I can't find anything that would cause this and I was just wondering if any of you guys have come across this before.
Thanks in advance
Rich
|
|
|
|
|
I think there is a problem in your code. Try to debug, after you close certan window. Or try recreate this child window.
quote:
behaviour only happens on certain machines
If you use same setting for this child window then all child would have same problem
|
|
|
|
|
That's what's confusing me. If it happened everywhere I would at least have a starting point.
|
|
|
|
|
Try to disable all your code. If error still exist, try to delete and create another child
|
|
|
|
|
I know that VS2005 is still in beta, and would like to dip my toe into the 2005 experience/IDE along with C++/CLI. I know that VS2005 was available for free download a while ago but unfortunatly i missed the boat! - I am not an MSDN subscriber? Is it possible for me to download the beta of VS2005 from anywhere else?
Cheers
|
|
|
|
|
|
thats excellent.. thankyou
|
|
|
|
|
|
HI all
I have the following problem .
I have a COblist member declared as protected in a class
CObList m_DataList;
Now in the virtual destructor of the same class
the data is cleaned up like this...
while(!m_DataList.IsEmpty())
delete m_DataList.RemoveHead();
...here assume the m_DataList has N elements ...
deleting N-1 elements is not a problem ..but when the Nth element is Removed from the m_DataList then Assertion occurs ..saying
Invalid heap pointer !! . I could not understand this at all ..
When the destructor is able to delete all the elements except the last one what could be the problem ?
Why should I face assertion when the last element is deleted ?
Can someone help me pls ?
dharani
redindian
|
|
|
|
|
First of all, you should investigate STL containers, MFC containers are crap.
Have you looked in the debugger to see the state of your pointers ? Is it possible that the problem is that you're stuffing IN an invalid pointer, and you don't see it until you try to delete it ?
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
I will give the vertices of outer polygon, the code need to generate the vertices of inner polygon , with given tolerance for e.g. 50 mt.
|
|
|
|