0

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,527
  • 31
  • 84
  • 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
3

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,795
  • 3
  • 27
  • 20
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
  • 131,802
  • 30
  • 241
  • 343
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
  • 339,232
  • 124
  • 596
  • 636
1

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

A *a = new A();
Matti Virkkunen
  • 63,558
  • 9
  • 127
  • 159
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,324
  • 4
  • 36
  • 61