|
Hello all. I'm having a terrible headache with a simple class... I have this class CTransfer, and I have two constructors:
<br />
class CTransferFileInfo;
class CTransferConfig;
<br />
CTransfer::CTransfer(CTransferFileInfo* pInfo, CTransferConfig* pConfig)<br />
{<br />
m_pFileInfo = pInfo;
m_pConfig = pConfig;
}<br />
<br />
CTransfer::CTransfer()<br />
{<br />
CTransfer::CTransfer(new CTransferFileInfo, NULL);<br />
}<br />
<br />
void CTransfer::SetFileInfo(CTransferFileInfo* pInfo)<br />
{<br />
if (m_pFileInfo) delete m_pInfo;<br />
m_pInfo = pInfo;<br />
}<br />
<br />
void CTransfer::SetConfig(CTransferConfig* pConfig)<br />
{<br />
if (m_pConfig) delete m_pConfig;<br />
m_pConfig = pConfig;<br />
}<br />
It does not seem to be rather difficult, is it? Well, the headache comes here. In another class, I create a CTransfer object with the default constructor, and call immediatly SetFileInfo, something like:
<br />
CTransfer* pTransfer = new CTransfer();<br />
pTransfer->SetFileInfo(pNewFileInfo);
So the problem goes in SetFileInfo. I debugged and realized that, when deleting m_pFileInfo... the member variable has random memory assigned! Worse, the variable m_pConfig, which is supposed to be NULL, has also a random memory address!
The thing goes worse. I debugged, and when entering CTransfer::CTransfer(CTransferFileInfo*, CTransferConfig*), it assigns the correct values to the variables. But after returning, the variables goes mad and change their value into a random memory address!
It's the first thing that this happens to me. Please, I'd appreciate any help. Thanks.
|
|
|
|
|
Kith Kahnan wrote:
CTransfer* pTransfer = new CTransfer();
What value do m_pFileInfo and m_pConfig have after this statement? I would guess they need to be set to NULL in CTransfer 's default constructor.
"Opinions are neither right nor wrong. I cannot change your opinion of me. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
This are the values:
m_pFileInfo = 0x008d3de0
m_pConfig = 0x008c7550
and pTransfer = 0x008d3d30
then, m_pFileInfo = m_pConfig = 0xcdcdcdcd
Thanks for your help.
|
|
|
|
|
I guess that the sentence CTransfer::CTransfer(new CTransferFileInfo(), NULL) is actually creating another instance of CTransfer... and of course, after leaving the method, the destructor is called... I guess I'd have to write much code anyway...
|
|
|
|
|
What happens when you just do this...
CTransfer::CTransfer()
{
m_pFileInfo = new CTransferFileInfo();
m_pConfig = NULL;
}
rather than this silliness...
CTransfer::CTransfer()
{
CTransfer::CTransfer(new CTransferFileInfo, NULL);
}
|
|
|
|
|
That "silliness" you call saves me a lot of coding. The contructor has too much initialization code. Actually, it is working, but i really don't want to code that much (because this problems is going on in other classes, also with too much initialization code).
Greetings.
|
|
|
|
|
Than I would suggest you have a specific method that both constructors call to initialize your data.
Read this:
http://www.brpreiss.com/books/opus4/html/page599.html[^]
What might be happening is that the custom constructor inits the variables but when the body of the default constructor runs those variables are set to system define defaults (i.e. random noise). I could well be wrong about this, but calling a constructor from within your default constructor seems a little cheesy to me.
|
|
|
|
|
Please take into account that calling a constructor inside another constructor is just illegal C++. The fact that it seems to work is just mere luck. C++ does not allow to do this. (Incidentally, this feature is being discussed for inclusion into the next revision of the C++ standard, under the name forward constructor.)
A valid alternative, however, is the following, which uses the so-called placement new:
#include <new>
...
CTransfer::CTransfer()
{
new (this) CTransfer(new CTransferFileInfo, NULL);
} All in all, having a common initialization function, as has been suggested, is probably clearer. Hope this helps.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
Want a Boost forum in Code Project? Vote here[^]!
|
|
|
|
|
Joaquín M López Muñoz wrote:
Please take into account that calling a constructor inside another constructor is just illegal C++.
Calling a base class constructor from a derived class constructor, would that be illigal to, or do you have to call them like this:
<br />
ClassB : public ClassA<br />
{
};<br />
<br />
ClassB::ClassB() : ClassA(), SomeMemberClass1(), SomeMemberClass2()<br />
{<br />
}<br />
Multiply it by infinity and take it beyond eternity and you'll still have no idea about what I'm talking about.
|
|
|
|
|
You have to call the constructors on the initializaer list:
ClassB::ClassB() : ClassA(), SomeMemberClass1(), SomeMemberClass2()
{
}
ClassB::ClassB()
{
ClassA::ClassA();
SomeMemberClass1::SomeMemberClass1();
SomeMemberClass1::SomeMemberClass2();
}
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
Want a Boost forum in Code Project? Vote here[^]!
|
|
|
|
|
Thanks,
I guess that I've got some classes to re-write...
Multiply it by infinity and take it beyond eternity and you'll still have no idea about what I'm talking about.
|
|
|
|
|
|
Does anyone know if it is possible to subclass the list header for an IShellView so that it can be custom drawn. This is easy with an MFC list control, but I have not been able to find a way to do this with the IShellView. I like the built in features of the IShellView, but would like to custom draw the list headers to match the rest of my program.
Thanks,
Nick
|
|
|
|
|
Currently I have a string [zPart] which would look something like the following “*(-2)”
I need to isolate everything inside the brackets “( …)” so it can be used later (in this case the –2).
My main goal is to replace * by an integer and perform the arithmetic inside the “(…)” on it. This will generate my finale value.
Any clues on how this could be implemented?
|
|
|
|
|
The answer depends largely on what compiler you are using.
If you are stuck using ansi-C there is a well known set of functions that are used for such string parseing needs. Look here[^]
MFC,.NET etc, generally provide similar functionality as part of their framework.
|
|
|
|
|
is this[^] help ?
TOXCCT >>> GEII power
|
|
|
|
|
Currently I have a string [zPart] which is either “*” or an integer value.
To differentiate between the two I do the following:
if (zPart == "*")
do something
else
do something else
This works great for now however I want to expand the functionality by allowing [zPart] to be something other then just “*”, for example “*(-2)”.
So I need a way to say the following [pseudo-code]:
if (zPart has a “*” in it)
do something
else
do something else
How would one implement this in C++?
Thanks,
|
|
|
|
|
|
Im new to C++ so sorry for this braindead question but (probably because its so basic) I cant find a solution to it. I have a Win32 app and I would like it to fill up the entire screen without a title bar. Is there something I can do when I call the CreateWindow function to make my app do this? If not how would I go about this?
Thanks!
|
|
|
|
|
|
Hello,
I just got a rude awakening when creating an app in my new VB.net IDE: anything created there requires that the end user download and install the ENTIRE .net framework! This wasn't the case in my VB 6.0.
So, does this also apply to VC++.net? Will VC++.net allow me to statically link the libraries so that I don't kill my users with a burdoning installation?
thanks,
JennyP
|
|
|
|
|
C++ is the only language that the .NET compiler can natively compile. That is to say, unless you use managed C++ ( and why would you ? ), there's no need ( and indeed no use ) in installing the .NET framework on a client machine.
Christian
I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
|
|
|
|
|
Do someone have some message flow (something like PostMessage with WM_CHAR) to simulate copy/paste (CTRL-C CTRL-V) within an application ?
tx anyway
GuimaSun
www.nexsun.com.br
NEXSUN TechZone
|
|
|
|
|
Are you talking about the WM_COPY and WM_PASTE messages?
"Opinions are neither right nor wrong. I cannot change the opinion you have of me. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
David, the problem with these messages is it just works in a edit control or combo box or some control that treats them, so AFAIK I have to simulate CTRL-C/CTRL-V which works with any situation
Thanks anyway.
|
|
|
|