Torrwin
asked on
Simple Class Question
Hello,
I am an experienced VB.NET programmer, trying to get a handle on C++ (console only, not Visual C++). I am having a problem with using classes.
In VB.NET you can globally declare an instance of a class that's not initialized. Then, later on, when an event occurs you can initialize it. (e.g. MyString = New String) It will then execute the code in the constructor.
How can I accomplish this in C++?
Thanks,
-Torrwin
I am an experienced VB.NET programmer, trying to get a handle on C++ (console only, not Visual C++). I am having a problem with using classes.
In VB.NET you can globally declare an instance of a class that's not initialized. Then, later on, when an event occurs you can initialize it. (e.g. MyString = New String) It will then execute the code in the constructor.
How can I accomplish this in C++?
Thanks,
-Torrwin
int _tmain(int argc, _TCHAR* argv[])
{
//Declare a global instance of the File class
File myFile;
cout << "Please select one of the following options:\n";
cout << "1) Setup a File\n";
cin >> iChoice;
if (iChoice == 1)
{
//This next line doesn't work in C++, what should it be?
myFile = New File;
}
else
{
cout << "Invalid choice.\n";
}
return 0;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
BTW, see also http://www.cplusplus.com/doc/tutorial/dynamic/ for more on that issue.
Make sure to initialize the pointer to 0 when declaring it. Or, in jkr's code, the delete at the end of main might cause unexpected results.