-1

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?

Moeb
  • 10,247
  • 28
  • 81
  • 110
  • 6
    Please! Go read a book about C++! Read about automatic vs dynamic allocation. Btw, there is NO garbage collection in C++... – ALOToverflow Apr 15 '10 at 17:48

6 Answers6

10

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?

  • @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. – Moeb Apr 15 '10 at 17:47
  • 5
    @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 –  Apr 15 '10 at 17:49
  • @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 Apr 15 '10 at 17:56
2

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
  • 4,725
  • 3
  • 26
  • 19
  • `A()` creates a temporary object with *value-initialization*, not necessarily with default constructor. – AnT Apr 15 '10 at 17:44
2

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
  • 128,551
  • 28
  • 227
  • 331
1

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
  • 327,498
  • 120
  • 583
  • 623
1

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

A *a = new A();
Matti Virkkunen
  • 61,328
  • 9
  • 119
  • 152
1

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
  • 4,204
  • 4
  • 34
  • 61