Difference between pointer to object.

 

Hello developer, I'm new here. I read code base and not found answer to my question. 
What is the difference with

Foo *foo2=new Foo("foo2");

and 

Foo* foo2=new Foo("foo2");


And when are they used?


Regards

Simon Anderson

 

Foo*␣name, foo␣*name, foo␣*␣name, or for*name are all exactly the same.

If you create a handle to a class object, you are responsible for their deletion. Use them only if you have no other choice.

 
So it doesn't matter where the * symbol placed? As long as the class is there, and the pointer is there, it will work?
 
Simon Anderson Hamonangan:
So it doesn't matter where the * symbol placed? As long as the class is there, and the pointer is there, it will work?

Correct it comes down to personal style. 

 
nicholi shen:

Correct it comes down to personal style. 

Thank you for the answer.
Now, why we used declaration of an object as pointer, and what is it's purpose compared to the syntax below :

Foo *foo2=new Foo("foo2");

Why not just simple declaration like :

Foo foo2("foo2");

What's the difference between both?
 
Simon Anderson Hamonangan:

Thank you for the answer.
Now, why we used declaration of an object as pointer, and what is it's purpose compared to the syntax below :

Foo *foo2=new Foo("foo2");

Why not just simple declaration like :

Foo foo2("foo2");

What's the difference between both?

https://stackoverflow.com/questions/3673998/what-is-difference-between-instantiating-an-object-using-new-vs-without

What is difference between instantiating an object using new vs. without
What is difference between instantiating an object using new vs. without
  • 2010.09.09
  • manzy704manzy704 1,02122 gold badges88 silver badges33 bronze badges
  • stackoverflow.com
Aside from dynamic memory allocation, is there a functional difference between the following two lines of code: I am assuming of course that a Time(int, int, int) ctor has been defined. I also realize that in the second case t will need to be deleted as it was allocated on the heap. Is there any other difference?
 
Simon Anderson Hamonangan: What's the difference between both?

The latter, you are responsible for their deletion.

 
William Roeder:

The latter, you are responsible for their deletion.

By the latter, do you mean this ? 

Foo *foo2=new Foo("foo2");

 
Simon Anderson Hamonangan:

By the latter, do you mean this ? 

Foo *foo2=new Foo("foo2");

He does.

 
Simon Anderson Hamonangan:

By the latter, do you mean this ? 

Foo *foo2=new Foo("foo2");

My coding experience has got me working on the same issues like you do. Why not join my public  project so we help each other learn?

Find Ultimate_AI in MetaEditor's public projects and join. I'll be glad.