Mailinglist Archive: opensuse-programming (96 mails)

< Previous Next >
Re: [suse-programming-e] Undepricated gcc Hello World?
  • From: Jerry Feldman <gfeldman@xxxxxxxxx>
  • Date: Mon, 31 Mar 2003 08:06:30 -0500
  • Message-id: <20030331080630.242cc738.gfeldman@xxxxxxxxx>
On Mon, 31 Mar 2003 06:17:16 -0500
"Steven T. Hatton" <hattons@xxxxxxxxxxxxxxxxxx> wrote:

> On Sunday 30 March 2003 04:57 pm, Jerry Feldman wrote:
> ...
> > Then to print the string, you may want to create a friend function
> > so that you can do something like:
> >
> > HelloClass hw("Hello, World!");
> >
> > cout << hw << endl;
> > This should be in one of your books.
>
> This looks a lot like the MyClass.toString() of Java.
The way you do it is to define a friend function.
friend ostream &operator<<(ostream &os, HelloClass &hw) {
os << hw.str;
return os;
}

Now you can output as above.

> C++ seems to be rife with opportunities to hang yourself with the
> available rope. One concern I have is to learn 'safe' coding in C++.
> I know there are all kinds of pitfalls that Java simply doesn't allow
> you to get near. Most of these have to do with pointers and memory
> management
Java is an excellent language whose objectives were to some extent to
remove some of the problem areas in C (and C++):
dynamic memory allocation and deallocation is something Java does with
garbage collections.
In C and C++, integral data types are not defined as a fixed size:
short must be at least 16 bits but may be larger.
int may be as small as 16 bits or larger. (The int data type was
intended to be the most efficient integral such that it was the register
size. On a 64 bit machine it should be 64 bits, but when Digital came
out with the Alpha, they decided that so much code assumes as 32 bit
int, that they kept int as 32 bits).
longs may be 32 bits or larger.

On typical 64 bit machines, such as Tru64 Unix, Linux on the Alpha chip,
longs and pointers are 64 bit. In Java, the language defines the size of
these variables such that a long is 64 bits (period).

Remember that the C language was designed as an OS implementation
language, not as a business application language. Java was originally
developed as an embedded language. C++ was designed to support OO. As
you lament, Bjarne does not agree with some of the direction the
language has taken. However, C++ is extremely powerful. You need to
understand things like multiple inheritance, templates, overloading, and
a few other elements. C++ also has a large body of class libraries, such
as the STL.

WRT: Safe coding. You need to have a basic understanding of C, pointers,
and automatic variables. Also, there are many products in C++ like
JBuilder. I suggest you first learn C++, then use code generators or
other tools that hide the details from you.
--
--
Gerald Feldman <gfeldman@xxxxxxxxx>
Boston Computer Solutions and Consulting
ICQ#156300 PGP key id:C5061EA9
PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9

< Previous Next >