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.

4
  • Look up pointers. Try not to dynamically allocate memory when possible, or at least use a vector/smart pointer. Commented Oct 16, 2012 at 13:07
  • 5
    This is basic knowledge, see The Definitive C++ Book Guide and List Commented Oct 16, 2012 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. Commented Oct 16, 2012 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. Commented Oct 16, 2012 at 13:14

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

So do I need to initialise my class first and then when I refer to it later use *myClass?
@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.
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.

Comments

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.

1 Comment

it is more commonly called "asterisk" instead of "star".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.