Where do I create my objects?

Started by
5 comments, last by Zahlman 17 years, 10 months ago
Here is a chunk of pseudo-C++ code:

#include <iostream>
#include "test.h"

// Function prototypes
void Menu();

int main()
{	
        Menu();

        NeatClass myClass;

	return 0;
}

void Menu()
{
	int menuChoice;

	cout << "1. Blah" << endl;
	cout << "2. Woo" << endl;
	cin >> menuChoice;

	// Good ol' crappy switch
	switch (menuChoice)
	{
		case 1:
			myClass.DoStuff();
			break;
	}
}

NeatClass is defined in test.h. If I want the Menu function to be able to use it though, I have to put it before Main, right? Doesn't that mean it's a global and aren't globals bad? So what's the proper place to create an instance of my class so that both Main and Menu (and any other functions) can use it? Sorry for the stupid question.
Advertisement
A better solution would be to pass the object to the function as a parameter:

#include <iostream>#include "test.h"// Function prototypesvoid Menu(NeatClass);int main(){	        NeatClass myClass;        Menu(myClass);	return 0;}void Menu(NeatClass nc){	int menuChoice;	cout << "1. Blah" << endl;	cout << "2. Woo" << endl;	cin >> menuChoice;	// Good ol' crappy switch	switch (menuChoice)	{		case 1:			nc.DoStuff();			break;	}}


Hope that helps!
Try this...

#include <iostream>#include "test.h"// Function prototypesvoid Menu(NeatClass *);int main(){	        NeatClass * myClass = new NeatClass();        Menu(myClass);        delete myClass;	return 0;}void Menu(NeatClass * neat){	int menuChoice;	cout << "1. Blah" << endl;	cout << "2. Woo" << endl;	cin >> menuChoice;	// Good ol' crappy switch	switch (menuChoice)	{		case 1:			beat->DoStuff();			break;	}}


That should work better for you.

theTroll
I would suggest passing it as a pointer instead of a class to prevent the overhead of passing the whole class.

theTroll
If the class has many members, it would indeed be better to not pass the whole class. Since we're using C++, however, that would be better accomplished with pass by reference. Even if you did use pass by pointer, there is no reason to allocate space for the object with new.

#include <iostream>#include "test.h"// Function prototypesvoid Menu(const NeatClass&);int main(){	        NeatClass myClass;        Menu(myClass);	return 0;}void Menu(const NeatClass& nc){	int menuChoice;	cout << "1. Blah" << endl;	cout << "2. Woo" << endl;	cin >> menuChoice;	// Good ol' crappy switch	switch (menuChoice)	{		case 1:			nc.DoStuff();			break;	}}


Note that if the DoStuff member function is not const, you will have to remove the const in the parameter list.
Awesome. Thanks for the help guys.

Is there any reason I'd want to pass by pointer instead of by reference?
*usually*, no. Pointers are a lower-level construct, and normally cannot gain you any performance advantage (they *may* be a *disadvantage*, for the reason that using a reference is more descriptive, and therefore the optimizing compiler has more information about what you're trying to do). The general guideline is "use references when you can; use pointers when you have to" (credit Marshall Cline, author of the C++ FAQ Lite). There are many guidelines in C++ of the "use X when you can; use Y when you have to" form, actually; and 'Y' happens to be 'pointers' (or something related) in a lot of cases ;)

However, you might pass by pointer when:

- because of external design forces, the calling code generally has a pointer "at hand" rather than an object instance. One of the reasons to use the reference is to avoid writing referencing operators (address-of, the '&' prefixes) all over the calling code; but if you find yourself writing dereferencing operators instead ('*' prefix on a pointer variable), you might want to rethink things.

- It is meaningful to provide an "invalid object" (or "no object"). The function must then check for the pointer null-ness, and handle that case appropriately. A reference can't validly refer to null, or to a bad memory location. (A similar rule applies for returning references vs. pointers: you might want to return a pointer if it is useful to be able to return NULL to indicate an error. However, you should in these cases also consider if you should be throwing an exception on error instead.)

This topic is closed to new replies.

Advertisement