DrUg13 <livefast@dieyoung.com> writes:
[color=blue]
> On Tue, 03 Feb 2004 03:03:27 GMT, "Victor Bazarov"
> <v.Abazarov@comAcast.net> wrote:
>[color=green]
> >"DrUg13" <livefast@dieyoung.com> wrote...[color=darkred]
> >> In java, this seems so easy. You need a new object
> >> Object test = new Object() gives me exactly what I want.[/color]
> >
> >You are just can't want anything else in Java...
> >[color=darkred]
> >> could someone please help me understand the different ways to do the
> >> same thing in C++. I find my self sometimes, trying
> >>
> >> Object app = Object();[/color]
> >
> >This is OK. 'app' is an automatic object. You could do the same
> >thing without the '=' sign.
> >
> > Object app;
> >
> >declares (and often defines) an object of type 'Object' and default-
> >initialises it.
> >[color=darkred]
> >> Object *app = Object();[/color]
> >
> >This is most likely nonsense. You declared a _pointer_ to an object of
> >type Object and tried initialising it using a temporary of type Object.
> >Unless type Object has a user-defined conversion to Object*, it won't
> >work.
> >[color=darkred]
> >> Object app = new Object();[/color]
> >
> >This is the inverse of the above. You declared (and probably defined)
> >an _object_ of type Object and are trying to initialise it using the
> >'new' expression (which returns a pointer). Unless Object type has
> >a special constructor that takes an argument of type Object* (a pointer
> >to Object), it won't work.
> >[color=darkred]
> >> randomly till something works... and this is bad because Im confused
> >> what is happening, and the differences.[/color]
> >
> >Have you tried a C++ book? If yes, which one? If not, why not?[/color]
> I'm a CS senior in College... ANd I collect C++ books and subscribe to
> safari. Some of my favorites are Deitel C++ programming,[/color]
Deitel & Deitel seems comforting and charming at first, but is in
fact riddled with errors and misconceptions.
[color=blue]
> OReilly -
> C++ in a Nutshell 2003, and my favorite Addison Wesley - The C++
> Programming Language - 3rd Edition written by Bjarne himself..[/color]
This last is the best of the 3. But you might need a different
approach; try Koenig & Moo's _Accelerated C++_ .
[color=blue][color=green]
> >
> >C++ is a complicated language that cannot be simply guessed. If you
> >really want to learn C++, _study_ it.[/color]
> I'm always studing c++. Java just seems so easy, I think it is
> confusing me.[/color]
Java has a good many features which seem similar to C++ features, but
due to radically different context, have suprisingly different
implications for programs. Don't write C++ the way you write
Java, and dont write Java the way you write C++.
[color=blue]
>
>[color=green]
> >
> > class Object {};
> > int main()
> > {
> > Object obj; // declares and defines an instance of Object
> > // that has _automatic_ storage duration and
> > // will go away when the block in which it is
> > // defined, closes
> >
> > Object *pobj = new Object; // declares and defines a pointer
> > // to Object and initialises it
> > // by creating a _dynamic_ object
> > // that will need to be destroyed at
> > // the end of your program by using
> > delete pobj; // ... the 'delete' keyword
> > }
> >[color=darkred]
> >> Someone please help...
> >> As a java programmer, I really like the new keyword.[/color]
> >
> >Liking or disliking keywords is not a valid reason to use or not to
> >use them.
> >[/color]
> I just like the simplicity of it.[/color]
C++ new may seem similar, but the context is quite different:
(a) C++ allows user-defined types on the stack, as members, etc -
meaning many objects do not and should not be allocated with
new. This is unlike java which requires all objects of
user-defined type to allocated with new.
(b) C++ does not have garbage collection (well, the standard
allows it, and there is the boehm collector
http://www.hpl.hp.com/personal/Hans_Boehm/gc/ but I don't know
of an implementation that provides it by default.)
(c) C++ pointers are both more powerful and more error-prone than
java references.
[color=blue][color=green]
> >Take my advice: get a good book on C++ and study.
> >Victor
> >[/color]
> Thanks Victor for taking the time to write such a detailed response.
> You have cleared up som econfusion. I can tell you really know your
> C++.
> HEre is my ebook collection, which one is your favorite??[/color]
[snip]
www.accu.org has reviews of many of these.