Solved

Simple Class Question

Posted on 2009-04-08
3
190 Views
Last Modified: 2012-05-06
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


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;
}

Open in new window

0
Comment
Question by:Torrwin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 24098856
In your cade, you would use the 'new' operator along with a pointer, e.g.
int _tmain(int argc, _TCHAR* argv[])
{
        //Declare an instance of a File class pointer
         File* myFile;
 
        cout << "Please select one of the following options:\n";
        cout << "1) Setup a File\n";
        
        cin >> iChoice;
 
        if (iChoice == 1) 
        {
                  
                 myFile = new File;
        }
        else
        {
                cout << "Invalid choice.\n";
        }
 
        delete myFile; // required to not create memory leaks
 
        return 0;
}

Open in new window

0
 
LVL 86

Expert Comment

by:jkr
ID: 24098892
BTW, see also http://www.cplusplus.com/doc/tutorial/dynamic/ for more on that issue.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 24099749
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.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question