Jump to content
  • Advertisement
Sign in to follow this  
DividedByZero

Classes and use of 'New'

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

Hi guys,

 

In the past it has always been a habit for me to create classes like this;

 

SomeClass ctest=new SomeClass();

 

And accessing the member stuff with ->

 

I know that using this method it allocates memory which must be deleted, delete ctest.

 

Apart from using the period for accessing the member variable/functions, what is the difference with just doing this;

 

SomeClass ctest;

 

Why would you use one method over the other?

 

Thanks in advance :)

Share this post


Link to post
Share on other sites
Advertisement

Your first example would be just a pointer which you give later some "life".

something like this is need if you want create dynamically a new object. 

 

For example you have class Car and ask the user how many cars he/she wants

create; how would you do solve that problem? write everytime a new program

or do you want create just dynamically a new Object of type Car with the new operator?

int count;
std::vector<Car*> cars;

//Ask user the amound of cars he/she wants
std::cout<<"Hello Human! How many car Objects do want that i create for you: \n";
std::cin>>count;

for(int i = 0; i < count;i++)
{
     Car* Example = new Car;
     Cars.push_back(Example)
}

//It was too early for meunsure.png  EDIT:

And because you create a class it's not necessary to delete it explicit because classes have a destructor which do this job for you, ....

 

That's wrong .... you need to call the dtor to destroy the object with the operator "delete". Such a silly mistake

 

 ...of course you should set the pointer to NULL in the end. explicit deleting you need if you allocating memoryfor  e.g. an array of size X. An array doesn't have an destructor so you have to do this job per hand....obviously 

Edited by exOfde

Share this post


Link to post
Share on other sites

Thanks for the reply. I am tryingto get my head around it smile.png

 

Couldn't you push the non 'new' example on to a vector as well though?

Technically speaking, yes, you could.  However, if you don't allocate the pointer before pushing it into the vector, it will remain unallocated.  This causes undefined behaviour and is asking for a world of hurt.

Edited by ByteTroll

Share this post


Link to post
Share on other sites

That's the thing. if you not know the amount you need. you cannot do it. of course you could save only the pointers but with just pointers you "cannot do much" 

Edited by exOfde

Share this post


Link to post
Share on other sites

Ah ok starting to get it.

 

Last stupid question I promise smile.png

 

By just going SomeClass cTest shouldn't the program allready know how much to allocate though? As, it should know the sizeof(SomeClass).

 

Or does this just simply not happen here?

 

[edit]

phil_t ninja'd me.

 

OK, now I am confused again LOL

 

And yes I did leave out * in my example :)

Edited by lonewolff

Share this post


Link to post
Share on other sites

Ah ok starting to get it.

 

Last stupid question I promise smile.png

 

By just going SomeClass cTest shouldn't the program allready know how much to allocate though? As, it should know the sizeof(SomeClass).

 

Or does this just simply not happen here?

What you are doing by "SomeClass cTest" is called Instantiatation.  When you instaniate an object, memory will automatically be allocated and deallocated for you (in the case the object being declared in a function)

Share this post


Link to post
Share on other sites

So, in escence it all boils down to memory efficiency right?

 

If the Class is declared globally (not that you would) as an example, the memory is used for the lifetime of the application.

 

But, if you use 'new' you can free up memory at any time you like by 'delete'ing them.

 

Is this about right?

 

Thanks for your input too guys! All upvoted smile.png

 

[edit]

 

Your first example would be just a pointer which you give later some "life".

something like this is need if you want create dynamically a new object.

 

For example you have class Car and ask the user how many cars he/she wants

create; how would you do solve that problem? write everytime a new program

or do you want create just dynamically a new Object of type Car with the new operator?

int count;
std::vector<Car*> cars;

//Ask user the amound of cars he/she wants
std::cout<<"Hello Human! How many car Objects do want that i create for you: \n";
std::cin>>count;

for(int i = 0; i < count;i++)
{
Car* Example = new Car;
Cars.push_back(Example)
}

And because you create a class it's not necessary to delete it explicit because classes have a destructor which do this job for you, of course you should set the pointer to NULL in the end. explicit deleting you need if you allocating memoryfor e.g. an array of size X. An array doesn't have an destructor so you have to do this

job per hand.

 

Isn't the last sentence incorrect though.

 

The destructor wont be called unless the instance is deleted.

Edited by lonewolff

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!