Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: New operator

Reply
Thread Tools

Re: New operator

 
 
Phil
Guest
Posts: n/a
 
      07-20-2003
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
>


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      07-20-2003

"vijay" <(E-Mail Removed)> wrote in message
news:bfdhve$a7i$(E-Mail Removed)...
>
> "Phil" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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


 
Reply With Quote
 
 
 
 
vijay
Guest
Posts: n/a
 
      07-20-2003

"Phil" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
Stuart Golodetz
Guest
Posts: n/a
 
      07-20-2003
vijay <(E-Mail Removed)> wrote in message
news:bfdigt$cc9$(E-Mail Removed)...
>
> "John Harrison" <(E-Mail Removed)> wrote in message
> news:bfdi95$dfspf$(E-Mail Removed)-berlin.de...
> >
> > "vijay" <(E-Mail Removed)> wrote in message
> > news:bfdhve$a7i$(E-Mail Removed)...
> > >
> > > "Phil" <(E-Mail Removed)> wrote in message
> > > news:(E-Mail Removed)...
> > > > 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")?


 
Reply With Quote
 
Phil
Guest
Posts: n/a
 
      07-20-2003
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" <(E-Mail Removed)> wrote in message
> news:bfdi95$dfspf$(E-Mail Removed)-berlin.de...
>
>>"vijay" <(E-Mail Removed)> wrote in message
>>news:bfdhve$a7i$(E-Mail Removed)...
>>
>>>"Phil" <(E-Mail Removed)> wrote in message
>>>news:(E-Mail Removed)...
>>>
>>>>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
>
>


 
Reply With Quote
 
Jacques Labuschagne
Guest
Posts: n/a
 
      07-20-2003
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.

 
Reply With Quote
 
Stuart Golodetz
Guest
Posts: n/a
 
      07-20-2003
Phil <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> > news:bfdi95$dfspf$(E-Mail Removed)-berlin.de...
> >
> >>"vijay" <(E-Mail Removed)> wrote in message
> >>news:bfdhve$a7i$(E-Mail Removed)...
> >>
> >>>"Phil" <(E-Mail Removed)> wrote in message
> >>>news:(E-Mail Removed)...
> >>>
> >>>>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
> >
> >

>



 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      07-20-2003
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What are the key differences between operator new and operator new[]? xmllmx C++ 6 02-03-2010 04:11 PM
Allocation with new operator and Destroying with free operator saikishore.vanga@wipro.com C++ 8 12-22-2005 01:35 PM
operator*(Foo) and operator*(int) const: ISO C++ says that these are ambiguous: Alex Vinokur C++ 4 11-26-2004 11:46 PM
Operator overloading on "default" operator John Smith C++ 2 10-06-2004 10:22 AM
Q: operator void* or operator bool? Jakob Bieling C++ 2 03-05-2004 04:27 PM



Advertisments