Re: Quick question on object instantiation!



OKAY.... sorry ...sorry ....sorry....

I had to do:

pShape->operator ++();

Now it works.....

I thankyou Victor! And wish you a nice day!

--
Best regards
Robert


"Robby" wrote:

Thankyou Victor....

Although this takes me to the second parft of my question.

I did include the object instantiation in my WndProc and made it static.
However please consider the following code, which is an extension of the
first part of my question:

Here is the complete code:
=========================================
class Shape
{
public:
Shape(); //Default constructor
Shape(Shape&); //Copy constructor
~Shape(); //Destructor

int GetCounter() const {return iCounter;}
void operator++(); //Operator overloading

private:
int iCounter;
};

void Shape::operator ++()
{
iCounter++;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
WPARAM wParam, LPARAM lParam)
{

static Shape *pShape = new Shape;

switch(message)
{

case WM_COMMAND:
++pShape; //Increment iCounter variable using the increment operator
return 0;


case WM_DESTROY:
delete pShape;
pShape = 0;
PostQuitMessage(0);
return 0;
}

...Other code...
=====================================================

The actuall program is much larger than the above example, however this is
all we need to debug the problem.

Everytime I click on a button, the WM_COMMAND handler gets executed, and
therefore, I would like it to increment the class member variable "iCounter"
by one by using the increment operator overloading method. However when I put
a breakpoint on the following line:

++pShape;

and follow the programs execution.... its like as if it doesn't even go to
its associated method which is:

void Shape::operator ++()
{
iCounter++;
}

Also, when I exit the application, I get some "Debug assetion fail error"!

I am copying the book's examples, except that my object is on the heap and
theirs isn't. Could that have something to do with it?

Confused.....

Can you tell me what I am doing wrong!

Thankyou in advance!

--
Best regards
Robert


"Victor Bazarov" wrote:

Robby wrote:
In Windows programming, is it bad practice or uncommon to instatiate
an object outside WndProc and then using the object in WndProc.

Don't know how [un]common this is, but I've used it (or, rather, some
variation of it).

Please consider the following code:
===========================================

class Shape
{
public:
Shape(); //Default constructor
Shape(Shape&); //Copy constructor
~Shape(); //Destructor

int GetCounter() const {return iCounter;}
void operator++();

private:
int iCounter;
};

Shape *pShape = new Shape; //Object instatiated outside WndProc

I usually make it 'static' and place it inside the 'WndProc' itself.
Then there is much less temptation to try to change it (or access it)
from some other function.


LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
WPARAM wParam, LPARAM
lParam) {

WM_PAINT:
pShape->SetTitles(hdc,rect); //Object now manipulated in WndProc
return 0;

case WM_DESTROY:
delete pShape; //Object deleted only when I exit
application

That's fine. My usual stuff wasn't necessarily in a global WndProc,
but in some other Procs, usually DlgProc. The main thing is not to
forget to set it to 0 after deletion (just in case).

PostQuitMessage(0);
return 0;

.....WndProc code.....

}

===================================================

The thing is that, I would like my object to stay in scope as long as
my program is running. However if I instantiate my object in WndProc,
then every time I return from a handler within my WndProc function, I
loose the data stored in the members of my class. So I figure if I
instantiate it outside WndProc, the class members would be
preserved.... right or not?

Declare the pointer 'static' and place it _inside_ WndProc.


I just would like to know if this type of coding is acceptable in
VC++?

As long as it serves your needs, who's to tell you that it isn't?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask



.



Relevant Pages

  • Something with callbacks and object instance and such...
    ... main objects WndProc I try to read or write to a protected var and it ... LRESULT CALLBACK MainWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Quick question on object instantiation!
    ... I did include the object instantiation in my WndProc and made it static. ... int GetCounterconst {return iCounter;} ... WPARAM wParam, LPARAM lParam) ... However if I instantiate my object in WndProc, ...
    (microsoft.public.vc.language)
  • Re: Quick question on object instantiation!
    ... I did include the object instantiation in my WndProc and made it static. ... int GetCounterconst {return iCounter;} ... WPARAM wParam, LPARAM lParam) ... However if I instantiate my object in WndProc, ...
    (microsoft.public.vc.language)
  • Re: Quick question on object instantiation!
    ... I did include the object instantiation in my WndProc and made it static. ... int GetCounterconst {return iCounter;} ... WPARAM wParam, LPARAM lParam) ... However if I instantiate my object in WndProc, ...
    (microsoft.public.vc.language)
  • Re: Compiler error C2664
    ... > LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, ... > class WindowClass: public WNDCLASSEX ...
    (comp.lang.cpp)