views:

123

answers:

6

Isn't

 A a = new A();   // A is a class name

supposed to work in C++?

I am getting:

conversion from 'A*' to non-scalar type 'A' requested

Whats wrong with that line of code?


This works in Java, right?

Also, what is the correct way to create a new object of type A in C++, then?

+1  A: 

You need A* a= new A();

new A(); creates and constructs an object of type A and puts it on the heap. It returns a pointer of that type.

In other words new returns the address of where the object was put on the heap, and A* is a type that holds an address for an object of type A

Anytime you use the heap with new you need to call an associated delete on the address.

Brian R. Bondy
+1  A: 

new gets you a pointer to the created object, therefore:

A *a = new A();
Matti Virkkunen
+9  A: 

No, it isn't. The new operation returns a pointer to the newly created object, so you need:

A * a = new A();

You will also need to manage the deletion of the object somewhere else in your code:

delete a;

However, unlike Java, there is normally no need to create objects dynamically in C++, and whenever possible you should avoid doing so. Instead of the above, you could simply say:

A a;

and the compiler will manage the object lifetime for you.

This is extremely basic stuff. Which C++ text book are you using which doesn't cover it?

anon
@Neil Butterworth: I just felt so confident that it is correct while writing it. I think it is because thats how we do it in Java.
Amoeba
@cambr Java and C++ have almost nothing in common, apart from trivial syntactical similarities. Your Java experience won't be of much use in learning C++, which is why you need to read a good book - see http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list
anon
@cambr: Java has abstractions C++ just can't get away with, for good or ill. Almost all Java variables are actually pointers, from a C or C++ point of view, so it doesn't need to be specified. C and C++ have both dynamic data objects referred to by pointer, and local objects that are not referred to by pointers. Going from Java to C++ is going to involve learning some new concepts.
David Thornley
+2  A: 

new A() returns a pointer A*. You can write

A a = A();

which creates a temporary instance with the default constructor and then calls the copy constructor for a or even better:

A a;

which just creates a with the default constructor. Or, if you want a pointer, you can write

A* a = new A();

which allows you more flexibility but more responsibility.

clahey
`A()` creates a temporary object with *value-initialization*, not necessarily with default constructor.
AndreyT
+2  A: 

The keyword new is used when you want to instantiate a pointer to an object:

A* a = new A();

It gets allocated onto the heap.. while when you don't have a pointer but just a real object you use simply

A a;

This declares the object and instantiate it onto the stack (so it will be alive just inside the call)

Jack
+1  A: 

First, new returns a pointer, so you need to declare the a's type as 'A*'. Second, unlike Java, you don't need parenthesis after A:

A* a = new A;

Alex Jenter