6

Possible Duplicate:
The Definitive C++ Book Guide and List

What does the * mean when initialising a class? Normally in AS3 I would do this:

MyClass myClass = new MyClass

But I have seen this in c++

 MyClass *myClass = new MyClass

What is the star for? I've seen it used sometimes and not others.

Dada
  • 6,313
  • 7
  • 24
  • 43
panthro
  • 22,779
  • 66
  • 183
  • 324
  • Look up pointers. Try not to dynamically allocate memory when possible, or at least use a vector/smart pointer. – chris Oct 16 '12 at 13:07
  • 5
    This is basic knowledge, see [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – MSalters Oct 16 '12 at 13:08
  • 3
    Why so many downvotes? He couldn't have googled it, you can't google *, he would have needed to already know it's a pointer in order to google it. – sashoalm Oct 16 '12 at 13:11
  • 2
    @satuon I didn't downvote but if doesn't even know what a `*` in C++ he should pick up a textbook before asking questions. – P.P Oct 16 '12 at 13:14

3 Answers3

5

The asterisk in C++ means many things depending on its place in the program. In this specific instance, it modifies the meaning of myClass to be a pointer to an instance of MyClass, rather than an instance of MyClass.

The difference between the two is that the lifetime of an instance ends when it goes out of scope, while an instance that you allocate and reference through a pointer remains valid even after a pointer goes out of scope.

It is valid to have a declaration like this:

MyClass myClass; // no "new"

In this case, it is not necessary to use new, but the instance's life time is tied to the scope of the variable myClass.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • So do I need to initialise my class first and then when I refer to it later use *myClass? – panthro Oct 16 '12 at 13:10
  • @user1013512 In C++ you have several options, as far as dealing with classes goes. You can use `new` to allocate an instance dynamically; in this case, the asterisk is necessary, and then you use `->` operator instead of a dot `.` to access members of the class. You must also call `delete myClass` when you are done with the object. You can also skip `new` altogether and use the dot `.`, but the object will be deleted as soon as its variable goes out of scope. – Sergey Kalinichenko Oct 16 '12 at 13:16
2

An asterisk stand for a pointer. A pointer is a memory adress. When you write MyClass *myClass, you create a pointer to the beginning of the bits range where your new MyClass is created.

Creating an object with new, you become responsible of it life cycle. YOU will need to delete it when you dont need them anymore, whereas creating MyClass myClass; will be destroy when exiting the scope where you create it. You still can access myClass memory adress even when written this way, using & operator.

If you need more explaination, try to get one of those books.

Community
  • 1
  • 1
tomahh
  • 13,441
  • 3
  • 49
  • 70
2

It's called a pointer. If you're using a C++11 compatible compiler, you can do the following:

auto myClass = std::make_shared<MyClass>();

If you were using a "raw" pointer, you would need to manually delete it when you are finished with the memory, with the shared_ptr, this isn't necessary.

Mark Ingram
  • 71,849
  • 51
  • 176
  • 230