I just destroyed the email I sas crafting and so this one will be a little more to-the-point. I mentioned before, Bruce Eckel's book is a great (free) place to start C++. It will answer all the questions below.
http://www.linuxguruz.org/ebooks/eckel/TICPP-2nd-ed-Vol-one.zip
http://www.linuxguruz.org/ebooks/eckel/TICPP-2nd-ed-Vol-two.zip


On Sat, 11 Aug 2012, Leslie S Satenstein wrote:
If you use the G++ compiler,  you can program with C++ and avoid classes,
have efficient code and except for the canned classes (namespace std) and
some others, gain as much performance as if coding in C.
Well, if you don't include it you won't include it. Just like in C you can use math, stdlib, string, etc. But if you don't include them they won't be in your code.

structures, unions, still work in C++.
Structs are classes in C++. So, a class with no methods is a struct with default private member variables. Which seems kind of useless... class myClasss { public: int a; int b; }; is equivalent to struct myStruct { int a; int b; };

I am not a strong C++ programmer, but if I declare a class, can I declare a
pointer of that class type and assign it to NULL or to any other object of
the same class?
Yes.  myClass* myclassptr = new myClass( blah1 );
delete myclassptr;
myclassptr = null;
myclassptr = new myClass( blah2 );A

Can I do a memset(class_pointer, 0, sizeof(class));  To wipe out a class
with mixed binary and asciiz string variables, some of which are public and
some protected?
Yes, you can. private member variables just means that regular access is restricted. But when you go memsetting you go around the protections c++ gives you. There was this whole argument about how c was somehow 'unsafe,' which is totally ridiculous if you ask me (I know you didn't, but there it is). Programming is not safe. In fact, if you could design a safe language where you couldn't shoot yourself in the foot, I bet it'd be useless. Programming isn't easy. Yes, memsetting a class to zero is really really stupid. But maybe just maybe there's a legitimate reason for doing so in this case. The other 99.999999% of the time, just delete myclassptr; Or better yet, use smart pointers (see "shared_ptr"). I have included a sample test program and the output at the bottom of this email.

Via constructors, I believe I would have to do an initialization of each
class variable, one by one.

Is there a sizeof(class name) operator?  fwrite( object, number of objects,
sizeof object, file pointer0  I am just asking as my C++ books don't talk
about this.

Yes for sizeof, yes for fwrite, but don't program in C if your filename ends in .cpp. Instead you open a file stream, overload the << operator for your class, and go: myfilestream << myobject1 << myobject2 << etc.


What I do like in C++ is the extension of the C printf sprintf, fprintf
functions.
What do you mean? If you're using *printf in a C++ program I can almost certainly point to where you're "doing it wrong." Yes it works. Will it make any c++ programmer who comes along afterward want to light a candle of satanic worship and curse your family for generations? That depends. << and >> are your friends. int myint; myint << weirdObject; //converting a weirdobject to an int.

My frustrations with C. If I have error messages in English, my variables
may be in  a,b,c sequence. If I switch languages, they would be in a,c,b
sequence or even c,a,b sequence.  this grammer language dependent (english,
french, spanish) problems appears addressed with C++ or is it?
That is up to you. If you have error messages that are retrieved depending on what language your user can understand, you'll probably use an error handler object of some type. Adding the capacity to re-order messages is up to the programmer, but is certainly not impossible. Difficulty depends on the way things were implemented initially. Same goes for a C implementation though.

Related to another topic. C coding style.
When I make my pointers that are malloc()ed global (above main() ), then I
have a way for a global function   exit() to free memory.

As well I can allocate memory in a sub function, retrieve the information in
the calling function, and do a free() from the caller, if I have to. If I
allocate memory in the sub-function, and forget to do a free() I leave an
orphan. But  I can do something too when I call exit(nn).  As all the
pointers to malloc'd areas are global, I can do some nice cleanup. (I write
security stuff, and I want to wipe out my areas before the free() or before
the program exits.) Many times when anomalies are detected in sub-functions,
we issue the longjump, code and I try to avoid this use.
  C++ has the try() { stuff}, which I like.

Yeah, C++ has try. Try this on for size though: try{ myClass* myptr = somefunction(); myptr->method(); } catch( ... ) { //gah what to do!!!} Has myptr been created properly? Should we check for that? Or just delete it? That's why smart pointers save so much headache. Don't malloc, don't free, instead use new/delete. And smart pointers.

Maybe the sweet C program that was custom crafted to a particular application ran the fastest, but while the C dev is spending hours/days/weeks trying to rewrite and debug the version 2.0 with that weird requirement that throws everything into madness, you can be sure that the C++ change will come with fewer headaches (if it was coded well to begin with).

I used to work with people who used Fortran because it was the FASTEST. Sure, there are edge cases where it's faster. But the cost of maintaining a huge Fortran (or was it FORTRAN) mess of code (and I do mean mess) is non-zero. And trying to modify it is frightful at best. Oh, right, and these people didn't know how to order loop iterations to minimize cache hits. So their code ended up being slower than it could have been in C (or Ada or Pascal or Turing...). I guess the important thing is to know your language, be careful, don't use too many "tricks," and remember that premature optimization really is the root of all evil. Or at least the cause of most of my headaches.

Now I can sit back and wait for all the people I've unknowingly pissed off for not mentioning Java or Forth to reply scathingly.


Regards

 Leslie


==memset.cpp===========
#include <iostream>
class abPrint {
    int a;
    int b;
public:
    abPrint(int a, int b):a(a),b(b){};
    void print() {
std::cout << "The ints are: a<<" << a << ">> and b <<" << b <<">>.\n";
    }
};

int main() {

    abPrint* pap = new abPrint( 1, 5 );
    pap->print();
    memset(pap, 0, sizeof(abPrint));
    pap->print();

    exit(0);
}
===end============


Output:
The ints are: a<<1>> and b <<5>>.
The ints are: a<<0>> and b <<0>>.
_______________________________________________
mlug mailing list
[email protected]
https://listes.koumbit.net/cgi-bin/mailman/listinfo/mlug-listserv.mlug.ca

Reply via email to