Sign in to follow this  

Where do I create my objects?

This topic is 4199 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
A better solution would be to pass the object to the function as a parameter:


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

// Function prototypes
void 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!

Share this post


Link to post
Share on other sites
Try this...


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

// Function prototypes
void 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

Share this post


Link to post
Share on other sites
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 prototypes

void 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.

Share this post


Link to post
Share on other sites
*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.)

Share this post


Link to post
Share on other sites

This topic is 4199 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this