Link to home
Start Free TrialLog in
Avatar of F-J-K
F-J-KFlag for Canada

asked on

Is It Good to Depend on Pointers When Creating an Object? Why?

I have a large class the has over 10 member functions, assume i name it CCar. What is better way to create the object?

CCar example1;
example1.burnFuel();?

Or

CCar *example2;
example2 = new CCar();
example2->burnFuel();?

1. I know the pointer is better, but is it good to use always (Assume i have 7 large classes)? If no, why?

2. How can i make my pointer points to the function, but with guaranteeing that no changes can be made by mistake? I know i have to put const somewhere when creating the object, can you show the proper way? I want to ensure example2 does not change anything in burn fuel, if it did, i should get compiler error.

3. When i include the header #include"CCar.h", the preprocessor will resolve all the header, etc, then program will start compiling. When pre-processor resolved all the headers, does not this increase the size of the current file? If yes, then whats the point of using objects as a pointer because we already copied whats in headers into our current file! right? Is there a way we can make include points to CCar.h?

4. When pre-processor deals & bring all the code in CCar.h into the current file, what happens to the implementation CCar.cpp?

5. In the implementation CCar.cpp we must include CCar.h & every definition function header will have a scope void CCar::burnFuel() {......}. Rgiht? OK, when we include CCar.h into main() as an example, then how CCar.h will believe its already have CCar.cpp (CCar.cpp by itself needs to include CCar.h, so main() as well will need to have a definition for CCar.h declaration). In other words, how can CCar.h in main() know that its already been definied in CCar.cpp while CCar.cpp needs to include CCar.h & the scope. So when you include CCar.h in main() how CCar.h can judge that it already been defined?

6. Based on question 5, what if i make two implementation for CCar.h? Will this work? Why?

Just feel free to answer any question you know its answer...

I hope to hear from you...
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of F-J-K

ASKER

Thanks, i will read it later & let you know if i got a question
1. A pointer is not "better" in general, it just does not limit the lifetime of your object to the declaration scope. In fact, it can be even dangerous if you forget to call "delete" on it, since in that case, you will cause memory leaks.

2. I am not sure what you mean by "pointing to the function" - is that related to https://www.experts-exchange.com/questions/24055467/How-Can-I-Pass-Function-as-an-Argument-C-C.html ?If not, you might just use

CCar *example2 = new CCar();

But beware on the implications - you will not be allowed to change any members.

3. The file size is not affected at all - a header file serves declaration purposes only, which means that it is used to make the interface of your implementation known to the compiler, which also implies pointers to the object known to the compiler.

4. Nothing, it remains untouched.

5. That is usually done by using so called "header guars". See e.g. http://www.cs.niu.edu/~mcmahon/cs241/c241man/node90.html

6. Don't do this. Unless they are identical (and in that case you do not need two of them) this is likely to cause errors.
Avatar of F-J-K

ASKER

Thanks jkr! Both of your responses answered what goes in my mind...

This is a useful one http://www.cs.niu.edu/~mcmahon/cs241/c241man/node90.html

Avatar of F-J-K

ASKER

>>Why do you think the pointer is better ? Without context, that's difficult to determine.

Based on what i have learned, holding pointers to objects rather than the actual objects could result in a considerable space savings & this can be so helpful when dealing with array collection objects.

You're not saving space, since you have to store both the object and the pointer in memory, instead of just the object. What you are winning on however, is speed in case you need to pass the object somewhere. Copying a pointer takes less time than copying a big object.
Pointers indeed also help when iterating over a collection of objects. But then you didn't allocate any memory for the pointer - you just let it point to an already existing object.