We help IT Professionals succeed at work.
Get Started

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

F-J-K
F-J-K asked
on
316 Views
Last Modified: 2012-05-06
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...
Comment
Watch Question
CERTIFIED EXPERT
Top Expert 2009
Commented:
This problem has been solved!
Unlock 1 Answer and 6 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE