new to pointers

M

Michael Sgier

Hi
I want to get familiar with C/C++ with some base knowledge ot VB.NET. In
my init.cpp i want
to initalize my engineclass like so:
engine = new CSimpEngine();
but I get that error:
init.cpp:319: error: `engine ' undeclared (first use this function)
why? how do i need to declare engine? My questions might sound weird to
you but i'm only taking my
first C++ steps.
Secondly like that i destroy it?
CSimpEngine *engine = NULL;
Why i use "*" here? Where do i need to use the "*"?
on the other side i can access CSimpEngine functions like:
gameCamera->velocity += CVector(1.0, 0.0, 0.0)
Thanks for now and excuse my stupidity.
Regards Michael
 
J

John Harrison

Michael said:
Hi
I want to get familiar with C/C++ with some base knowledge ot VB.NET. In
my init.cpp i want
to initalize my engineclass like so:
engine = new CSimpEngine();
but I get that error:
init.cpp:319: error: `engine ' undeclared (first use this function)
why? how do i need to declare engine? My questions might sound weird to
you but i'm only taking my
first C++ steps.

Probably this

CSimpEngine* engine = new CSimpEngine();

But I have to say the if you don't know how to declare a variable
(engine is a variable) then you really shouldn't be thinking about
pointers yet.

In C++ all variables (pointers or not) must be declared before they are
used.
Secondly like that i destroy it?
CSimpEngine *engine = NULL;

No, this declares a variable and initialises it to NULL. It got nothing
to do with destroying anything.

You destroy the CSimpEngine object you created above like this

delete engine;

Note you do not destroy variables or pointers. What you destroy is an
object. 'delete engine' destroys the object that the engine variable is
pointing to.
Why i use "*" here? Where do i need to use the "*"?

You use * in two situations. To declare a pointer varaible

X* pointer_to_x;

and to access an object that a pointer points to, e.g.

cout << *pointer_to_x; // outputs whatever 'pointer_to_x' is pointing to

-> can also be used for similar purposes, x->m is (more or less) short
for (*x).m
on the other side i can access CSimpEngine functions like:
gameCamera->velocity += CVector(1.0, 0.0, 0.0)
Thanks for now and excuse my stupidity.
Regards Michael

I think you need a good book. It's going to get a lot harder unless you
straighten out some of the misconceptions you have. Pointers are
difficult, you need good quality teaching.

john
 
R

Rolf Magnus

Michael said:
Hi
I want to get familiar with C/C++ with some base knowledge ot VB.NET. In
my init.cpp i want
to initalize my engineclass like so:
engine = new CSimpEngine();

Do you actually need to dynamically allocate it?
but I get that error:
init.cpp:319: error: `engine ' undeclared (first use this function)
why?

Probably, because 'engine' is not declared (surprise!).
how do i need to declare engine?

Well, e.g.:

CSimpEngine* engine;

That would define (and thereby declare) a pointer to a CSimpEngine.
My questions might sound weird to you but i'm only taking my first C++
steps.

Then you should get a good book about C++. Otherwise, it will take a lot
longer and be a lot harder for you to learn it than necessary.
Secondly like that i destroy it?
CSimpEngine *engine = NULL;

No. You don't. Actually, that would be a declaration of a CSimpEngine
pointer named engine and initialized as a null pointer.
Why i use "*" here?

I don't know why you use it. But in your example, it makes engine a pointer
to a CSimpEngine. If you leave out the *, it would be an instance of
CSimpEngine, though you probably couldn't initialize that with NULL.
Where do i need to use the "*"?

When you want to make a pointer to the specified class instead of an
instance of it.


PS: If you can't make heads or tails of what I wrote above, you definitely
need a good entry-level C++ book.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,141
Latest member
BlissKeto
Top