Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Does "void func(MyClass *& myCls)" make sense? (http://www.velocityreviews.com/forums/t289256-does-void-func-myclass-and-mycls-make-sense.html)

modemer 03-03-2005 05:18 PM

Does "void func(MyClass *& myCls)" make sense?
 
I saw someone use the following code:

void func(MyClass *& myCls)
{
myCls->test();
}

// call func():
func(new MyClass);

Some compiler could compile, but some not.

I don't understand the meaning of function prototype. I guess it's not
a stand C++ prototype. Please don't guess like me if you are not 100%
understanding it :-) because probably I'll have further question if
you answer.

Dietmar Kuehl 03-03-2005 05:47 PM

Re: Does "void func(MyClass *& myCls)" make sense?
 
modemer wrote:
> void func(MyClass *& myCls)
> {
> myCls->test();
> }


The above code is OK: it is a function taking a pointer by reference.
It is apparently unnecessary to pass the argument by reference,
though, as it is not changed. If would be a better example if it had
a body like this:

{
myCls = new MyClass();
}

> // call func():
> func(new MyClass);


However, no compiler shall accept this code with the above
declaration. The function's argument is a temporary pointer to an
object but temporaries cannot be bound to non-const references.
--
<mailto:dietmar_kuehl@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting


Victor Bazarov 03-03-2005 05:50 PM

Re: Does "void func(MyClass *& myCls)" make sense?
 
modemer wrote:
> I saw someone use the following code:
>
> void func(MyClass *& myCls)
> {
> myCls->test();
> }
>
> // call func():
> func(new MyClass);
>
> Some compiler could compile, but some not.
>
> I don't understand the meaning of function prototype. I guess it's not
> a stand C++ prototype. Please don't guess like me if you are not 100%
> understanding it :-) because probably I'll have further question if
> you answer.


There is no prototype here. The function 'func' as defined/declared,
takes one argument which is a reference to a non-const pointer to
an object of MyClass.

A Standard-compliant compiler should not compile

func(new MyClass);

because the 'new' expression returns an r-value and a reference to
a non-const pointer cannot be bound to an r-value. You can change
it to

void func(MyClass * const & myCls)
{
myCls->test();
}

then all compilers should compile

func(new MyClass);

, however, this will be a memory leak because the value of the pointer
obtained from 'new' is lost and the memory is never freed and the object
is never destroyed.

V

Pete Becker 03-03-2005 06:33 PM

Re: Does "void func(MyClass *& myCls)" make sense?
 
Victor Bazarov wrote:
>
> A Standard-compliant compiler should not compile
>
> func(new MyClass);
>


My usual comment: A Standard-compliant compiler must issue a diagnostic.
The standard doesn't say that this should not compile. Similarly,
Dietmar's "no compiler shall accept this code..." should be "no compiler
shall accept this code without issuing a diagnostic...". Once it issues
a diagnostic the compiler is free to do whatever the compiler writer
wants, including generating an executable file. Of course, the standard
doesn't say what such an executable file should do.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)

=?iso-8859-1?Q?Ali_=C7ehreli?= 03-03-2005 10:37 PM

Re: Does "void func(MyClass *& myCls)" make sense?
 
"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:V7IVd.49381$NC6.38428@newsread1.mlpsca01.us.t o.verio.net...
> modemer wrote:


> void func(MyClass * const & myCls)
> {
> myCls->test();
> }
>
> then all compilers should compile
>
> func(new MyClass);
>
> , however, this will be a memory leak because the value of the pointer
> obtained from 'new' is lost and the memory is never freed and the object
> is never destroyed.


That has been exactly my comment after reviewing some code at a previous
company.

It turned out that the member function (here 'test') was actually
registering the 'this' pointer in some static container and such objects
were in fact being deleted later on. Argh!

Memory leak or not, I still know that it's an error to design like that :)

Ali


modemer 03-04-2005 03:18 PM

Re: Does "void func(MyClass *& myCls)" make sense?
 
Victor,

Such appreciate your perfect explanation about "*&"!!!

"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:V7IVd.49381$NC6.38428@newsread1.mlpsca01.us.t o.verio.net...
> modemer wrote:
> > I saw someone use the following code:
> >
> > void func(MyClass *& myCls)
> > {
> > myCls->test();
> > }
> >
> > // call func():
> > func(new MyClass);
> >
> > Some compiler could compile, but some not.
> >
> > I don't understand the meaning of function prototype. I guess it's not
> > a stand C++ prototype. Please don't guess like me if you are not 100%
> > understanding it :-) because probably I'll have further question if
> > you answer.

>
> There is no prototype here. The function 'func' as defined/declared,
> takes one argument which is a reference to a non-const pointer to
> an object of MyClass.
>
> A Standard-compliant compiler should not compile
>
> func(new MyClass);
>
> because the 'new' expression returns an r-value and a reference to
> a non-const pointer cannot be bound to an r-value. You can change
> it to
>
> void func(MyClass * const & myCls)
> {
> myCls->test();
> }
>
> then all compilers should compile
>
> func(new MyClass);
>
> , however, this will be a memory leak because the value of the pointer
> obtained from 'new' is lost and the memory is never freed and the object
> is never destroyed.
>
> V





All times are GMT. The time now is 09:12 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.