Re: New operator

Discussion in 'C++' started by Phil, Jul 20, 2003.

  1. Phil

    Phil Guest

    Sorry.. in the second program I meant:

    void main()
    {
    A *a = new A();
    a->doSomething();
    }


    Thanks.

    -Phil


    Phil wrote:
    > Hi,
    >
    > Just need to clear a confusion...
    >
    > Suppose we have the following class:
    >
    > class A {
    > public:
    > A(){}
    > void doSomething() { do stuff... }
    > };
    >
    > Then suppose we have the following program:
    >
    > void main()
    > {
    > A a;
    > a.doSomething();
    > }
    >
    > How is the above program different from the following:
    >
    > void main()
    > {
    > A a = new A();
    > a.doSomething();
    > }
    >
    >
    > thanks a bunch!
    >
    > -Phil
    >
    Phil, Jul 20, 2003
    #1
    1. Advertising

  2. "vijay" <> wrote in message
    news:bfdhve$a7i$...
    >
    > "Phil" <> wrote in message
    > news:...
    > > Sorry.. in the second program I meant:
    > >
    > > void main()
    > > {
    > > A *a = new A();
    > > a->doSomething();
    > > }
    > >
    > >
    > > Thanks.
    > >
    > > -Phil
    > >
    > >
    > > Phil wrote:
    > > > Hi,
    > > >
    > > > Just need to clear a confusion...
    > > >
    > > > Suppose we have the following class:
    > > >
    > > > class A {
    > > > public:
    > > > A(){}
    > > > void doSomething() { do stuff... }
    > > > };
    > > >
    > > > Then suppose we have the following program:
    > > >
    > > > void main()
    > > > {
    > > > A a;
    > > > a.doSomething();
    > > > }
    > > >
    > > > How is the above program different from the following:
    > > >
    > > > void main()
    > > > {
    > > > A a = new A();
    > > > a.doSomething();
    > > > }
    > > >
    > > >
    > > > thanks a bunch!
    > > >
    > > > -Phil
    > > >

    > >

    > First one, obj created on the stack
    > and cesond caseobj is created on Heap ,
    > First one obj's destructor is called immeidately on function exit
    >
    > Second case , the obj destructor is called only when program comes to an
    > end,
    > Since u r creating in function main, it makes no difference since function
    > exit anf program exit is almost the same when viewed from class user POW
    > Vijay


    That is not true, objects destructor is never called in the second case.

    john
    John Harrison, Jul 20, 2003
    #2
    1. Advertising

  3. Phil

    vijay Guest

    "Phil" <> wrote in message
    news:...
    > Sorry.. in the second program I meant:
    >
    > void main()
    > {
    > A *a = new A();
    > a->doSomething();
    > }
    >
    >
    > Thanks.
    >
    > -Phil
    >
    >
    > Phil wrote:
    > > Hi,
    > >
    > > Just need to clear a confusion...
    > >
    > > Suppose we have the following class:
    > >
    > > class A {
    > > public:
    > > A(){}
    > > void doSomething() { do stuff... }
    > > };
    > >
    > > Then suppose we have the following program:
    > >
    > > void main()
    > > {
    > > A a;
    > > a.doSomething();
    > > }
    > >
    > > How is the above program different from the following:
    > >
    > > void main()
    > > {
    > > A a = new A();
    > > a.doSomething();
    > > }
    > >
    > >
    > > thanks a bunch!
    > >
    > > -Phil
    > >

    >

    First one, obj created on the stack
    and cesond caseobj is created on Heap ,
    First one obj's destructor is called immeidately on function exit

    Second case , the obj destructor is called only when program comes to an
    end,
    Since u r creating in function main, it makes no difference since function
    exit anf program exit is almost the same when viewed from class user POW
    Vijay
    vijay, Jul 20, 2003
    #3
  4. vijay <> wrote in message
    news:bfdigt$cc9$...
    >
    > "John Harrison" <> wrote in message
    > news:bfdi95$dfspf$-berlin.de...
    > >
    > > "vijay" <> wrote in message
    > > news:bfdhve$a7i$...
    > > >
    > > > "Phil" <> wrote in message
    > > > news:...
    > > > > Sorry.. in the second program I meant:
    > > > >
    > > > > void main()
    > > > > {
    > > > > A *a = new A();
    > > > > a->doSomething();
    > > > > }
    > > > >
    > > > >
    > > > > Thanks.
    > > > >
    > > > > -Phil
    > > > >
    > > > >
    > > > > Phil wrote:
    > > > > > Hi,
    > > > > >
    > > > > > Just need to clear a confusion...
    > > > > >
    > > > > > Suppose we have the following class:
    > > > > >
    > > > > > class A {
    > > > > > public:
    > > > > > A(){}
    > > > > > void doSomething() { do stuff... }
    > > > > > };
    > > > > >
    > > > > > Then suppose we have the following program:
    > > > > >
    > > > > > void main()
    > > > > > {
    > > > > > A a;
    > > > > > a.doSomething();
    > > > > > }
    > > > > >
    > > > > > How is the above program different from the following:
    > > > > >
    > > > > > void main()
    > > > > > {
    > > > > > A a = new A();
    > > > > > a.doSomething();
    > > > > > }
    > > > > >
    > > > > >
    > > > > > thanks a bunch!
    > > > > >
    > > > > > -Phil
    > > > > >
    > > > >
    > > > First one, obj created on the stack
    > > > and cesond caseobj is created on Heap ,
    > > > First one obj's destructor is called immeidately on function exit
    > > >
    > > > Second case , the obj destructor is called only when program comes to

    an
    > > > end,
    > > > Since u r creating in function main, it makes no difference since

    > function
    > > > exit anf program exit is almost the same when viewed from class user

    POW
    > > > Vijay

    > >
    > > That is not true, objects destructor is never called in the second case.
    > >
    > > john
    > >
    > > Thanks John for correcting me ,

    > I was writing with a Memory leak POW. That was a typo ..
    > vijay


    Probably a dumb question, but what do you mean by POW in this context? The
    only meaning I know for POW (and the only one I could turn up with a quick
    Google search) is "prisoner-of-war", which hardly fits with the rest of the
    sentence. I mean, we're all in favour of catching memory leaks, but for
    someone to actually "imprison" them might be going a little far... :)

    TIA :)

    Stuart.

    P.S. Do you mean POV ("point-of-view")?
    Stuart Golodetz, Jul 20, 2003
    #4
  5. Phil

    Phil Guest

    thanks a lot guys! can was say "a is an instance of A" for the following:

    A a;
    a.doSomething();

    What will we then say for the following:

    A *a = new A();
    a->doSomething();

    will we say the same for 'a' ? where does the term "reference" come in?
    Thanks!

    -Phil




    vijay wrote:
    > "John Harrison" <> wrote in message
    > news:bfdi95$dfspf$-berlin.de...
    >
    >>"vijay" <> wrote in message
    >>news:bfdhve$a7i$...
    >>
    >>>"Phil" <> wrote in message
    >>>news:...
    >>>
    >>>>Sorry.. in the second program I meant:
    >>>>
    >>>>void main()
    >>>>{
    >>>> A *a = new A();
    >>>> a->doSomething();
    >>>>}
    >>>>
    >>>>
    >>>>Thanks.
    >>>>
    >>>>-Phil
    >>>>
    >>>>
    >>>>Phil wrote:
    >>>>
    >>>>>Hi,
    >>>>>
    >>>>>Just need to clear a confusion...
    >>>>>
    >>>>>Suppose we have the following class:
    >>>>>
    >>>>>class A {
    >>>>> public:
    >>>>> A(){}
    >>>>> void doSomething() { do stuff... }
    >>>>>};
    >>>>>
    >>>>>Then suppose we have the following program:
    >>>>>
    >>>>>void main()
    >>>>>{
    >>>>> A a;
    >>>>> a.doSomething();
    >>>>>}
    >>>>>
    >>>>>How is the above program different from the following:
    >>>>>
    >>>>>void main()
    >>>>>{
    >>>>> A a = new A();
    >>>>> a.doSomething();
    >>>>>}
    >>>>>
    >>>>>
    >>>>>thanks a bunch!
    >>>>>
    >>>>>-Phil
    >>>>>
    >>>>
    >>>First one, obj created on the stack
    >>>and cesond caseobj is created on Heap ,
    >>>First one obj's destructor is called immeidately on function exit
    >>>
    >>>Second case , the obj destructor is called only when program comes to an
    >>>end,
    >>>Since u r creating in function main, it makes no difference since

    >>

    > function
    >
    >>>exit anf program exit is almost the same when viewed from class user POW
    >>> Vijay

    >>
    >>That is not true, objects destructor is never called in the second case.
    >>
    >>john
    >>
    >>Thanks John for correcting me ,

    >
    > I was writing with a Memory leak POW. That was a typo ..
    > vijay
    >
    >
    Phil, Jul 20, 2003
    #5
  6. Phil wrote:
    > thanks a lot guys! can was say "a is an instance of A" for the following:
    >
    > A a;
    > a.doSomething();


    Any time you create an object of type A (i.e. you cause one of its
    constructors to be called) you can point and say "look, that's an instance
    of A". It doesn't matter if it's a regular variable as above, a temporary
    variable or one created dynamically using operator new.

    >
    > What will we then say for the following:
    >
    > A *a = new A();
    > a->doSomething();
    >
    > will we say the same for 'a' ? where does the term "reference" come in?
    > Thanks!


    A reference is like an alias for something. If you have

    A a_var;
    A *a_ptr = &a_var; // pointer
    A &a_ref = &a_var; // reference

    Now there are three ways to get at the object called a_var:
    direct access -
    a_var.doSomething();
    indirect access through a pointer -
    a_ptr->doSomething();
    and through a reference (faster but less flexible than a pointer) -
    a_ref.doSomething();

    Do you have a book you're trying to learn from? If not, I suggest you get
    one. If you do but this isn't covered, burn it and then get another. :)

    --
    Good engineering is finding the correct wrench
    with which to pound in the right screw.
    Jacques Labuschagne, Jul 20, 2003
    #6
  7. Phil <> wrote in message news:...
    > thanks a lot guys! can was say "a is an instance of A" for the following:
    >
    > A a;
    > a.doSomething();
    >
    > What will we then say for the following:
    >
    > A *a = new A();


    No need for the brackets:

    A *a = new A;

    > a->doSomething();
    >
    > will we say the same for 'a' ? where does the term "reference" come in?


    Nope, because a is no longer an instance of A. Instead, a now points to an
    instance of A, or in other words: *a is an instance of A.

    HTH
    --
    Stuart Golodetz
    Please remove 'spamavoidance' from my address to reply by email
    (On the whole, replying in the newsgroup is preferable anyhow)

    > Thanks!
    >
    > -Phil
    >
    >
    >
    >
    > vijay wrote:
    > > "John Harrison" <> wrote in message
    > > news:bfdi95$dfspf$-berlin.de...
    > >
    > >>"vijay" <> wrote in message
    > >>news:bfdhve$a7i$...
    > >>
    > >>>"Phil" <> wrote in message
    > >>>news:...
    > >>>
    > >>>>Sorry.. in the second program I meant:
    > >>>>
    > >>>>void main()
    > >>>>{
    > >>>> A *a = new A();
    > >>>> a->doSomething();
    > >>>>}
    > >>>>
    > >>>>
    > >>>>Thanks.
    > >>>>
    > >>>>-Phil
    > >>>>
    > >>>>
    > >>>>Phil wrote:
    > >>>>
    > >>>>>Hi,
    > >>>>>
    > >>>>>Just need to clear a confusion...
    > >>>>>
    > >>>>>Suppose we have the following class:
    > >>>>>
    > >>>>>class A {
    > >>>>> public:
    > >>>>> A(){}
    > >>>>> void doSomething() { do stuff... }
    > >>>>>};
    > >>>>>
    > >>>>>Then suppose we have the following program:
    > >>>>>
    > >>>>>void main()
    > >>>>>{
    > >>>>> A a;
    > >>>>> a.doSomething();
    > >>>>>}
    > >>>>>
    > >>>>>How is the above program different from the following:
    > >>>>>
    > >>>>>void main()
    > >>>>>{
    > >>>>> A a = new A();
    > >>>>> a.doSomething();
    > >>>>>}
    > >>>>>
    > >>>>>
    > >>>>>thanks a bunch!
    > >>>>>
    > >>>>>-Phil
    > >>>>>
    > >>>>
    > >>>First one, obj created on the stack
    > >>>and cesond caseobj is created on Heap ,
    > >>>First one obj's destructor is called immeidately on function exit
    > >>>
    > >>>Second case , the obj destructor is called only when program comes to

    an
    > >>>end,
    > >>>Since u r creating in function main, it makes no difference since
    > >>

    > > function
    > >
    > >>>exit anf program exit is almost the same when viewed from class user

    POW
    > >>> Vijay
    > >>
    > >>That is not true, objects destructor is never called in the second case.
    > >>
    > >>john
    > >>
    > >>Thanks John for correcting me ,

    > >
    > > I was writing with a Memory leak POW. That was a typo ..
    > > vijay
    > >
    > >

    >
    Stuart Golodetz, Jul 20, 2003
    #7
  8. Phil

    Rolf Magnus Guest

    Phil wrote:

    > thanks a lot guys! can was say "a is an instance of A" for the
    > following:
    >
    > A a;
    > a.doSomething();


    Yes.

    > What will we then say for the following:
    >
    > A *a = new A();
    > a->doSomething();
    >
    > will we say the same for 'a' ?


    No. a is a pointer to an instance of A. You can more clearly see that if
    you change the first line to:

    A* a = new A();

    which better shows that the * is part of the type and not the name.

    > where does the term "reference" come in?


    In your example, nowhere. Only "dereference" might come in. in
    "a->doSomething();", the pointer a is dereferenced, which means you
    access the object that it points to, not the pointer itself.
    "a->doSomething();" is just a nicer looking way to write:

    (*a).doSomething();

    *a dereferences the pointer, so you are accessing the object it points
    to, and then you use . to access one of its members, like in your first
    example.
    Rolf Magnus, Jul 20, 2003
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dodo
    Replies:
    1
    Views:
    358
    Ron Natalie
    Aug 26, 2003
  2. Jef Driesen
    Replies:
    1
    Views:
    500
    Gernot Frisch
    Jan 19, 2005
  3. Replies:
    8
    Views:
    330
    Neil Cerutti
    Dec 22, 2005
  4. xmllmx
    Replies:
    6
    Views:
    413
    xmllmx
    Feb 3, 2010
  5. Replies:
    2
    Views:
    449
    Thomas 'PointedEars' Lahn
    Mar 11, 2008
Loading...

Share This Page