473,385 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

new to pointers

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
Sep 5 '05 #1
2 1266
Michael Sgier wrote:
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
Sep 5 '05 #2
Michael Sgier wrote:
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.

Sep 5 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Susan Baker | last post by:
Hi, I'm just reading about smart pointers.. I have some existing C code that I would like to provide wrapper classes for. Specifically, I would like to provide wrappers for two stucts defined...
3
by: ozbear | last post by:
This is probably an obvious question. I know that pointer comparisons are only defined if the two pointers point somewhere "into" the storage allocated to the same object, or if they are NULL,...
9
by: Mikhail Teterin | last post by:
Hello! I'd like to have a variable of a pointer-to-function type. The two possible values are of type (*)(FILE *) and (*)(void *). For example: getter = straight ? fgetc : gzgetc; nextchar...
12
by: Lance | last post by:
VB.NET (v2003) does not support pointers, right? Assuming that this is true, are there any plans to support pointers in the future? Forgive my ignorance, but if C# supports pointers and C# and...
14
by: Alf P. Steinbach | last post by:
Not yet perfect, but: http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01_examples.zip To access the table of...
92
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers,...
4
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
25
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void...
54
by: Boris | last post by:
I had a 3 hours meeting today with some fellow programmers that are partly not convinced about using smart pointers in C++. Their main concern is a possible performance impact. I've been explaining...
2
by: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.