Sign in to follow this  

The point of class and templates

This topic is 3575 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

What is the point of class and templates To be reusable and ??? 1) to write code quickly and reuse quickly? 2) to use without worry of how safe your code is. The base class will take longer to write and the code size may expand 3fold and may need more maintenance? But always safe. by not safe i mean it doesnt have bugs in special case but you can get unexpected results if you forget to init and other things along that line.

Share this post


Link to post
Share on other sites
If this isn't for a class, then the answer is that "until you need them you can't grasp their point, so they're effectively useless to you." If people would stop teaching things the students don't need right now, they might have a better understanding.

All code is a means of guaranteeing invariants. Functions, classes, templates and other reusable constructs allow you define a closed set of invariants for a given use, and then apply that to a new context. What this means is that instead of trying to make sure your dynamically allocated character array's memory is being properly managed while performing string tokenization (the memory management is not the task; tokenization is), you can simply use std::vector and std::list and rely on the invariants they already guarantee.

Share this post


Link to post
Share on other sites
AFAIK, the purpose of class is the same like function, that is to reduce complexity. It's better/easier to write small codes that works (and you can guarantee it will always works), than you have to write a really big code.

For templates, I suggest you to just try STL, and try to do the same thing with your own code(without templates), and feel the productivity difference. You will know why you need templates.

Share this post


Link to post
Share on other sites
no this isnt for a class (as in school)
i notice that half the time i write a class the other half is making it not crash and safe to error. This doubles my code size and preventing the code from crashing (ie return errors #) seems pointless because 1) the code would still not work if the coder is forgetting something 2) i'll probably be teh only person using it or if i hand someone else the code it will be documented. So there doesnt seem there is a point to prevent it from crashing and returning error #

should i do the way i prefer and not make it crash safe?

Share this post


Link to post
Share on other sites
You should learn to make your classes fail-safe by design, instead of hammering in safety once the class is designed. A class should be small and elegant, achieving its unique purpose in just a handful lines of code.

Share this post


Link to post
Share on other sites
Quote:
Original post by AcidZombie24
What is the point of class and templates

To be reusable and ???
1) to write code quickly and reuse quickly?
2) to use without worry of how safe your code is. The base class will take longer to write and the code size may expand 3fold and may need more maintenance? But always safe.

by not safe i mean it doesnt have bugs in special case but you can get unexpected results if you forget to init and other things along that line.


It can take a while to reach that point, but once you have a certain level of proficiency with classes, they will actually shorten your code, reduce maintenance work and not hinder performance.

Check out the RAII idiom for a 1-minute enlightenment on how classes can make your life easier, reduce code size and improve performance.
Wikipedia: Resource Acquisition Is Initialization

In this example, without classes, you have to remember to call fclose() yourself whenever the function returns. That's error-prone and means more maintenance work:

void foo() {
FILE *fp = fopen("file.dat", "rb");

// do something

if(error) {
fclose(fp);
throw std::runtime_error("Error xyz occured");
}

// do something more

fclose(fp);
}


If you had a 'File' class, the code would become much simpler:

void foo() {
File f("file.dat", "rb");

// do something

if(error)
throw std::runtime_error("Error xyz occured");

// do something more
}


When 'f' leaves the scope (either due to the exception or by normal return), the 'File' instance will be destroyed and the file will be closed. Less work, code becomes shorter.

-Markus-

Share this post


Link to post
Share on other sites

This topic is 3575 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this