about temporary object

Discussion in 'C++' started by cxf, Aug 3, 2008.

  1. cxf

    cxf Guest

    class A{
    public:
    A()
    {
    func(0);
    };
    virtual void func(int data)
    {
    printf("A1 :%d\n",data);
    }
    virtual void func(int data) const
    {
    printf("A2 :%d\n",data);
    }
    };

    A().func(1) invoke which function? ,why?
    I think it is " void func(int data) const", because A() return a
    temporary object, which should be const. But when it is executed with
    vc 6.0, it invokes "void func(int data)"!

    please help me, thank you!
     
    cxf, Aug 3, 2008
    #1
    1. Advertising

  2. cxf

    raicuandi Guest

    On Aug 3, 1:00 pm, cxf <> wrote:
    > class A{
    > public:
    >         A()
    >                 {
    >                        func(0);
    >                  };
    >         virtual void func(int data)
    >                 {
    >                         printf("A1 :%d\n",data);
    >                 }
    >         virtual void func(int data) const
    >                 {
    >                        printf("A2 :%d\n",data);
    >                 }
    >
    > };
    >
    > A().func(1) invoke which function? ,why?
    > I think it is " void func(int data) const", because A() return a
    > temporary object, which should be const. But when it is executed with
    > vc 6.0, it invokes "void func(int data)"!
    >
    > please help me, thank you!


    The line:

    A().func(1);

    Means 2 invocations of "func". The first one is going to be the non-
    const one, because its called from inside the constructor. The
    constructor is suppose to modify the instance, therefore it calls the
    non-const one.

    As for the second "func" call, its again the non-const one, for
    obvious reasons: "A()" returns an A, not a "const A".

    Observe:
    A().func(1);
    Output:
    A1 :0
    A1 :1

    Now:
    const A* a = new A(); // "a" is now declared const
    a->func(1);
    Output:
    A1 :0
    A2 :1

    However:
    A* a = new A(); // "a" NOT declared const, defaults to non-const
    a->func(1);
    So obviously:
    A1 :0
    A1 :1
     
    raicuandi, Aug 3, 2008
    #2
    1. Advertising

  3. cxf

    raicuandi Guest

    On Aug 3, 1:00 pm, cxf <> wrote:
    > I think


    This is where everything went wrong... :p
    Half a minute alone with a compiler always gets the last say though.
     
    raicuandi, Aug 3, 2008
    #3
  4. cxf

    James Kanze Guest

    On Aug 3, 6:00 am, cxf <> wrote:
    > class A{
    > public:
    > A()
    > {
    > func(0);
    > };
    > virtual void func(int data)
    > {
    > printf("A1 :%d\n",data);
    > }
    > virtual void func(int data) const
    > {
    > printf("A2 :%d\n",data);
    > }
    >
    > };


    > A().func(1) invoke which function?


    The non-const one.

    > ,why?


    Because it can. Literally: if all other things are equal, and
    two functions differ only by their const-ness, the compiler will
    prefer the non-const version whenever it can be called.

    > I think it is " void func(int data) const", because A() return
    > a temporary object, which should be const.


    Why? You asked the compiler to construct an A, not an A const.

    There is no way to request an A const directly, but you can
    return one:

    extern A f() ;
    extern A const g() ;

    f().func() ; // invokes non-const function
    g().func() ; // invokes const function.

    > But when it is executed with vc 6.0, it invokes "void func(int
    > data)"!


    As required.

    The issues are not trivial, and largely historically
    conditionned. But basically, a temporary is something called an
    rvalue: if it has non-class type, it has no cv-qualifiers in its
    type (but what you can do with it is restricted by the
    constraints concerning lvalue and rvalue of the different
    operators). If the rvalue has class type, however, things
    become more complicated, because const-ness (and volatile-ness)
    affect what you can do with it. Since "int f()" returns an int
    (and not an int const), "A f()" returns an "A", and not an "A
    const". However, while changing the declaration to "int const
    f()" does nothing but create confusion in the minds of the
    reader (because the return value is an rvalue of non-class type,
    and isn't cv-qualified), changing it to "A const f()" does have
    an effect (because despite being an rvalue, the temporary must
    still be a real object, with address, etc., and there are ways
    of getting that address).

    Finally, of course, the situation is additionally complicated
    because of the rule that you cannot use an rvalue (even if it
    has class type) to initialize a non-const reference, but you can
    use one (even if it doesn't have class type, and isn't an
    "object") to initialize a const reference (which means that,
    indirectly, you can get the address of the temporary).

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Aug 3, 2008
    #4
  5. cxf

    Rolf Magnus Guest

    cxf wrote:

    > class A{
    > public:
    > A()
    > {
    > func(0);
    > };
    > virtual void func(int data)
    > {
    > printf("A1 :%d\n",data);
    > }
    > virtual void func(int data) const
    > {
    > printf("A2 :%d\n",data);
    > }
    > };
    >
    > A().func(1) invoke which function? ,why?


    The non-const version, because A() is not const.

    > I think it is " void func(int data) const", because A() return a
    > temporary object, which should be const.


    You must be mixing that up with binding a reference to a temporary. In your
    case, no reference is involved, so it doesn't apply here.
     
    Rolf Magnus, Aug 3, 2008
    #5
    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. John Ky
    Replies:
    9
    Views:
    439
    John Ky
    Feb 23, 2004
  2. qazmlp
    Replies:
    3
    Views:
    387
    Claudio Puviani
    Mar 7, 2004
  3. Stefan Strasser
    Replies:
    2
    Views:
    8,168
    Victor Bazarov
    Mar 28, 2005
  4. Jess
    Replies:
    10
    Views:
    506
    James Kanze
    May 18, 2007
  5. Replies:
    7
    Views:
    3,224
    James Kanze
    Feb 12, 2008
Loading...

Share This Page