On Tue, 20 Mar 2012, Patricia Campbell wrote:

>From your email you are thinking in non OOP terms.  If you want to use C++
you will have to switch your thinking a bit.  I recommend Deitel & Deitel
C++ any version > 5 will probably do for
conceptshttp://www.amazon.com/How-Program-8th-Paul-Deitel/dp/0132662361

Great advice as always, Patricia. I'd recommend Bruce Eckel's "Thinking in C++" first and second volumes.
http://mindview.net/Books/TICPP/ThinkingInCPP2e.html

They're free, easy to read/follow, plenty of bite-sized examples, and I think much more intuitively organized than, say, the O'Reilly nutshell/programming books.

 If I can do sizeof(structure) to get its size in memory. Can I do the
same of a class.  sizeof(class) ?

Structs and classes are the same, the only difference is public vs private by default. But structs in C++ aren't the same as structs in C! Read a thorough, detailed intro book and you'll see why thinking of C++ as an extension to C is a bad idea. It is probably one of the greatest disservices to C programmers who want to learn C++, who think 'oh it's just some language extensions.' But sizeof works as expected.

One other question.  In C, I can do a memset(structure
address, 0, > sizeof(structure)), and that will zap the structure.

Don't do that! Even in C! Sure, for a struct that contains a pair of floats that's fine (some kind of point(x,y) struct). But what about a linked list implementation? If you have a linked list that cointains a {pointer to value, pointer to next item}, and you zap the head of your list, you didn't clean up the value or the other elements, just 2 pointers. What, you had thousands of items in that list? And no way to accesss them? Memory leak! Same holds true in c++, that's why you have a destructor that you write which goes through each element explicitly.

But more importantly, why are you memsetting it to zero (in C)? That makes no sense at all.. sure you could do it, but why not just set the struct's elements to zero? Or if you want to 'delete' it, just let it fall out of scope... Please share the point of that 'zap' and I or someone else can better explain an equivalent idea in C++.

Can I put the structure within a class and do the same?  I think I am
not able to do the memset(?class,0 sizeof(class))
clearout because of private variables.

Structs=classes. Sort of. But don't go memsetting like that. You're going to end up with really broken code really quickly. If you think you're going to save some cycles, you're wrong. Create a method, say 'clear()', that sets internal class/struct state to "zero", or somesuch.


Check out Eckel's books. Don't go into it thinking you can use any of your C knowledge (other than where to put a ';' or scope blocks inside '{' '}', etc). It's a totally different approach that's designed to make it easier to represent abstract concepts more efficiently than in procedural C. That doesn't mean the code is faster, although it often is for complex projects because it is more concisely defined than it could be in C.

Blah blah blah....
Sorry for the long post, I hope you find it helpful.
_______________________________________________
mlug mailing list
[email protected]
https://listes.koumbit.net/cgi-bin/mailman/listinfo/mlug-listserv.mlug.ca

Reply via email to