Class vs. PT(Class)

Hello! In the C++ version of Panda3D, what is the difference between:

Class

and:

PT(Class)

???

The same as in C++ the difference between

Class

and

Class *

Except that PT(Class) additionally manages reference counting. This is explained here:
https://docs.panda3d.org/1.10/cpp/programming/using-cpp/reference-counting

You can think of PT(Class) being similar to std::shared_ptr<Class>, since they more or less behave in the same way.

The link you have said shows this:

PT(TextNode) textNode = new TextNode("My Text");

But what about the CollisionHandlerPusher in the manuals, it shows:

PT(CollisionHandlerPusher) pusher = new CollisionHandlerPusher;

Why are there no brackets in the latter?

The former class is value-initialized and the latter is default-initialized. This is a very basic C++ syntax question; I would highly advise learning C++ before trying to use C++ with Panda, or you will be constantly tripping up over basic things like this.

I know C++, I just didn’t know this feature. I thought to default-initialize, we have to use:

CollisionHandlerPusher();

rather than doing it without brackets.

You can’t say “I know C++” when you don’t understand the difference between default-initialization and value-initialization.

If you use new T, default-initialization occurs, so classes or members without a default constructor will be left uninitialized. If you use new T(), value-initialization occurs, causing classes or members without a default constructor to be zero-initialized.

CollisionHandlerPusher has a default constructor which therefore gets invoked in either situation.

1 Like

Oh. So it works like that. Must have misread the C++ tutorial web page I was using.