|
You'll find that your object does not actually exist until the constructor returns. This could well be part of your problem. I don't believe you can use 'this' inside a constructor.
Christian
come on all you MS suckups, defend your sugar-daddy now. - Chris Losinger - 11/07/2002
|
|
|
|
|
If you "don't believe" try this:
class _A
{
public:
_A()
{
m_this = this;
};
_A *m_this;
void f()
{
printf("instance: %p\n",m_this);
}
};
void main()
{
_A a1,a2, *pa1,*pa2 ;
a1.f();
a2.f();
pa1 = new _A();
pa2 = new _A();
pa1->f();
pa2->f();
delete pa1;
delete pa2;
return;
}
soptest
|
|
|
|
|
Hmmm... well, I wonder what the rule was then ? I had some problem to do with assuming an item was existant in the constructor, when it was only after the constructor returned that it existed.....
Christian
come on all you MS suckups, defend your sugar-daddy now. - Chris Losinger - 11/07/2002
|
|
|
|
|
Actually one can use this in the constructor. The address is valid and does not change any more - so you can safely store it or pass it to someone else. It depends on how you use it.
You have to consider that all members you did not initialize already are still undefined. And virtual functions are not dereferenced as expected during constructor execution. While beeing constructed, the object behaves like "static" typed. All functions are executed as if the object is of the class which constructer is currently executed:
class A {
public:
A() {
vf();
}
virtual void vf() {
printf("A::vf() called\n");
}
};
class B : public A {
public:
virtual void vf() {
printf("B::vf() called\n");
}
};
void main ()
{
A a;
B b;
}
The output of this little program would be:
A::vf() called
A::vf() called
The reason for this is, that constructers are called in order of the inheritance graph beginning with the most general base class(es). (The constructer of the base class(es) is always executed before the constructor of the derived class is entered.) And the very first thing a constructor does is to init the vtable. So at the point in time the A part of a B instance is constructed, the vtable is still the one of class A .
--
Daniel Lohmann
http://www.losoft.de
|
|
|
|
|
That's it - it was the virtual function problem that bit me before.
Thanks.
Christian
come on all you MS suckups, defend your sugar-daddy now. - Chris Losinger - 11/07/2002
|
|
|
|
|
I had the similar problem some time ago. I need to call a virtual function from within base class constructor, which is of course impossible (cause the derriven class is not yet constructed).
You can write define or template function. Pass the type as the argument. After construction (operator new for instance), call required virtual member, then return newly created instance of the object.
This works fine for me.
Mukkie
|
|
|
|
|
u can do it, i am sure because i use the way often to pass an instance itself to manager (or whatever).
i.g.
class MyClass
{
public:
MyClass()
{
mng.Add(this);//Manager mng;
}
};
if u don't beleave, simplify ur code to do a test.
includeh10
|
|
|
|
|
It would be possible to sort of hide the constructor of the CVariable class, and to instead, have variables allocated by the CVariableManager class. Something like this maybe:
<br />
class CVariableManager;<br />
class CVariable()<br />
{<br />
private:<br />
friend class CVariableManager;<br />
CVariable( );<br />
~CVariable();<br />
<br />
public:<br />
};<br />
<br />
class CVariableManager<br />
{<br />
public:<br />
CVariableManager();<br />
~CVariableManager();<br />
<br />
CVariable * CreateVariable( );
void DeleteVariable( CVariable * p_poVariable );
};<br />
Then you can use the CVariableManager to actually manage the variable creation and deletion.
Chris Richardson
|
|
|
|
|
Compiler says it needs initialized. So how can I create it as a member variable and put its declaration in the (public)h file so everyone can access it?
Thanks,
ns
|
|
|
|
|
A member variable that needs to be initialised can be done like so
(in .h )
class Blah
{
SomeType m_st;
(in .cpp)
Blah::Blah() : m_st("initialisiation values"
{
etc.
However, for you to have a reference, it needs something to reference to. Is it a class variable, or a global ( in a namespace of course ).
Christian
come on all you MS suckups, defend your sugar-daddy now. - Chris Losinger - 11/07/2002
|
|
|
|
|
I was considering a class variable. So it seems like a big limitation that we cant declare class members that are a reference. I think I've seen stuff like
local function
{
someType xyz;
someType &abc;
abc=xyz (not abc=&xyz right?)
I have a locally declared & variable and wanted to use it in another function....
|
|
|
|
|
If it's a class variable I'd have thought it would work if you used an initialiser list as in my first reply.
Christian
come on all you MS suckups, defend your sugar-daddy now. - Chris Losinger - 11/07/2002
|
|
|
|
|
To declare a pointer to a type, use '*' instead of '&'.
for example:
long *mLongVar;
To make it accessible to everybody put it under 'public:'
|
|
|
|
|
Thats what I ended up doing....so thank you very much for confirming that my approach was correct.
ns
|
|
|
|
|
Hi all
I need to get server side system time from my computer
how to do this?
Thanks
|
|
|
|
|
u could use http://www.codeproject.com/internet/ndk.asp
for client server.
Then ask gently from the server to send its time, and server has to respond with its time into a CString for example
Papa
Murex Co.
while (TRUE)
Papa.WillLove ( Bebe ) ;
|
|
|
|
|
Use the command
net time \\server</font> where server is the name of the server from which you want to retrieve the time. This call can also be easily embedded into a program.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Use NetRemoteTOD
Carlos Antollini.
Sonork ID 100.10529 cantollini
|
|
|
|
|
I have a problem that I think might get sorted out if I forcibly make the OnItemChanged handler of my listctrl execute. Thing is if I called it like: myListCtrl.OnItemChanged( ...), I have no clue what to pass in in the argument list. How can one trigger the OnItemChanged event to run, if you "selected" (highlighted) an item with code, instead of by pressing on it? To select the key i used:
m_list1.SetItemState( 0,LVIS_SELECTED | LVIS_FOCUSED , LVIS_SELECTED | LVIS_FOCUSED);
This apparently doesnt run the OnItemChanged function....
Please help!
Thanks,
ns
|
|
|
|
|
I could do
pView->SendMessage(LVN_ITEMCHANGED, IDC_MYLISTCTRL,0) just guessing. ????
|
|
|
|
|
You can format the message yourself & send it, (don't forget, it takes a pointer to a return value!) but why trouble yourself unnecessarily? If all you're doing is checking for a change in selection, just create a separate method that gets called both from OnItemChanged() and directly when you change the selection programatically.
Shog9
--
Maybe Java is kind of like God, it "works in mysterious ways". It seems like your apps are running slowly, because in the backgroud Java is solving world hunger, or finding the cure to cancer.
- Ryan Johnston, Don't die java!
|
|
|
|
|
Hi,
I would like to have the small help button (the one that appears in the upper right of a dialog if its context property is set to true) take the user to a page in my help file that is relevant to the dialog they working with. Is there any way that the default "turn into a question mark pointer" function can be overridden to acheive this??
Thanks in advance!
Is it football season yet???
|
|
|
|
|
I'm trying to display the contents of list box from a dialog box in print preview. I'm having trouble implementing this. Can some one point me in the right direction.
Thanks
|
|
|
|
|
for each item in the list, get the string and use CDC::TextOut to draw it to the print preview DC.
-c
To explain Donald Knuth's relevance to computing is like explaining Paul's relevance to the Catholic Church. He isn't God, he isn't the Son of God, but he was sent by God to explain God to the masses. /. #3848917
|
|
|
|
|
Is it OK to use it? When is the object actually destroyed?
Michel
It is a lovely language, but it takes a very long time to say anything in it, because we do not say anything in it, unless it is worth taking a very long time to say, and to listen to.
- TreeBeard
|
|
|
|