Jump to content
  • Advertisement
Sign in to follow this  
darenking

SOLVED!!! Why do I have to do it like that - why can't I do it like this???

This topic is 4831 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

I'm creating an object then deleting it after accessing one of its methods. I'm doing it like this:
		Creator* creator = new Creator();
		if ( creator->File() )
		{
			delete creator;
		}


It looks daft to me, as it looks like I'm creating a pointer to an object then dereferencing it. Why can't I just create an object instead of a pointer to an object, I thought. So I tried it like this:
		Creator creator = new Creator();
		if ( creator.File() )
		{
			delete creator;
		}


...but I get an error, "converstion from 'Creator*' to non-scaler type 'Creator' requested". What can it all mean? Was just hoping to simplify things a little. [Edited by - darenking on August 30, 2005 8:18:35 AM]

Share this post


Link to post
Share on other sites
Advertisement
-> is the C++ (and C) shortcut for specifying members of classes that are pointed to. It is a shortcut, because the command that more accurately reflects what is going on is

(*creator).File()
but it makes code more readable. C/C++ does not allow you to use the . operator as a member-of-class-pointed-to reference because a pointer IS a variable.

Hope that helps,
Twilight Dragon

Share this post


Link to post
Share on other sites
With the 'new' keyword you explicitly request a pointer.

You can do it like this:
Creator creator;
creator.File();

Once creator is out of scope it is automatically deleted.

Share this post


Link to post
Share on other sites
new and delete are used only to allocate memory dynamically, therefore only for pointers. To create a simple instance of the class:

Creator creator();

Then you can use it and it is destroyed automatically when it goes out of scope.

Share this post


Link to post
Share on other sites
The new operator allocates memory and returns a pointer to the type you called the constructor for:

Creator* pCreator = new Creator();
pCreator->DoStuff();


You could do it the way you like only it does not involve a call to new. The object will be created:

Creator creator;
creator.DoStuff();


Greetz,

Illco

Share this post


Link to post
Share on other sites
With the 'new' keyword you explicitly request a pointer.

You can do it like this:
Creator creator;
creator.File();

Once creator is out of scope it is automatically deleted.

Share this post


Link to post
Share on other sites
new and delete only work with pointers. If you use the second version, you need not use new and delete at all because the compiler will allocate and deallocate stack memory for the variable for you.

Share this post


Link to post
Share on other sites
Yes, that is well and truly cleared up. It's just the same as declaring anything else, such as a string or an int. I'm such a dullard.

Off to snog a mallard.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!