Does "void func(MyClass *& myCls)" make sense?

Discussion in 'C++' started by modemer, Mar 3, 2005.

  1. modemer

    modemer Guest

    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.
     
    modemer, Mar 3, 2005
    #1
    1. Advertising

  2. 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:> <http://www.dietmar-kuehl.de/>
    <http://www.contendix.com> - Software Development & Consulting
     
    Dietmar Kuehl, Mar 3, 2005
    #2
    1. Advertising

  3. 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
     
    Victor Bazarov, Mar 3, 2005
    #3
  4. modemer

    Pete Becker Guest

    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)
     
    Pete Becker, Mar 3, 2005
    #4
  5. "Victor Bazarov" <> wrote in message
    news:V7IVd.49381$01.us.to.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
     
    =?iso-8859-1?Q?Ali_=C7ehreli?=, Mar 3, 2005
    #5
  6. modemer

    modemer Guest

    Victor,

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

    "Victor Bazarov" <> wrote in message
    news:V7IVd.49381$01.us.to.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
     
    modemer, Mar 4, 2005
    #6
    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. Phil Sandler

    NULLs from SQL--does this make sense?

    Phil Sandler, Aug 19, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    342
    Phil Sandler
    Aug 19, 2004
  2. Jason
    Replies:
    0
    Views:
    361
    Jason
    Oct 12, 2004
  3. Timo Nentwig
    Replies:
    31
    Views:
    1,222
    Chris Smith
    May 13, 2004
  4. milkyway

    Does this make sense?

    milkyway, Dec 2, 2004, in forum: Java
    Replies:
    0
    Views:
    386
    milkyway
    Dec 2, 2004
  5. milkyway

    Does this make sense?

    milkyway, Dec 2, 2004, in forum: Java
    Replies:
    2
    Views:
    345
Loading...

Share This Page