Sunday, August 2, 2009

How can I make a "global object" in C++ (VS2005) similar to how I would in VBA?

In VBA I have always creates global objects by defining them at the global level like this:





public myObject As myClass





How can I do something similar in Visual Studio 2005 with C++?





Currently I have a button, text box, main(), and an event handler for the button. Where do I define the object so that when the event handler is called it can always access to object?

How can I make a "global object" in C++ (VS2005) similar to how I would in VBA?
You do it essentially the same way. You declare the object outside the scope of any function, including main(). In some cases you might want to make it static.





This is, of course, in general an extremely poor design practice.





#include %26lt;iostream%26gt;





using namespace std;





class A


{


public:





A() {cout %26lt;%26lt; "hello\n";}


void print() {cout %26lt;%26lt; "print\n";}


~A() {cout %26lt;%26lt; "goodbye\n";}


};








A a;





int main(int argc, char *argv[])


{


a.print();


}
Reply:you declare public attributes of the class lis this





class myClass


{


public:





}


then you create an instance of that class in main.

spring flowers

No comments:

Post a Comment