Google Mail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
references and composition
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Follow-up To:
Add Cc | Add Follow-up to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers that you hear
 
Nick Valeontis  
View profile   Translate to Translated (View Original)
 More options 9 Nov, 00:01
Newsgroups: comp.lang.c++
From: "Nick Valeontis" <mouh...@frGGmail.gr[replace G to e]>
Date: Mon, 9 Nov 2009 02:01:29 +0200
Local: Mon 9 Nov 2009 00:01
Subject: references and composition
Hmmm, i have been playing around with cpp, but, honestly, I need some help
on this one...

Could someone explain to me why this does work as it does?

**************  **************  **************  **************
#include <iostream>
using namespace std;
class Value {
    public:
        int v;
        Value(int value) {
            cout << "const!";
            v = value;
        }

};

class T {
    public:
        Value& value;

        T(Value value):value(value) {

        }

};

int main(int argc, char** argv) {
    Value v(5);
    T t(v);

    cout << v.v;
    cout << t.value.v;

    //t.value.v++;        // output is 5555 !!?
    v.v++;                // output is 5566

    cout << v.v;
    cout << t.value.v;

}

**************  **************  **************  **************

while: ( the difference is on the constructor of T)

**************  **************  **************  **************
#include <iostream>
using namespace std;
class Value {
    public:
        int v;
        Value(int value) {
            cout << "const!";
            v = value;
        }

};

class T {
    public:
        Value& value;

        T(Value& value):value(value) {

        }

};

int main(int argc, char** argv) {
    Value v(5);
    T t(v);

    cout << v.v;
    cout << t.value.v;

    //t.value.v++;        // output is 5566
    v.v++;                // output is 5566

    cout << v.v;
    cout << t.value.v;

}

**************  **************  **************  **************

any idea?:/

thanks

 -Nick


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Alf P. Steinbach  
View profile   Translate to Translated (View Original)
 More options 9 Nov, 05:04
Newsgroups: comp.lang.c++
From: "Alf P. Steinbach" <al...@start.no>
Date: Mon, 09 Nov 2009 06:04:01 +0100
Local: Mon 9 Nov 2009 05:04
Subject: Re: references and composition
* Nick Valeontis:

> class T {
>     public:
>         Value& value;

>         T(Value value):value(value) {

>         }

The scope of the formal argument name 'value' starts at its point of declaration.

In the mem-initializer 'value(value)' the first 'value' is a mem-initializer-id.
As such it's looked up in the class scope yielding T::value, and for this the
formal argument's name collision doesn't matter. The second is an ordinary
expression, and so lookup of that name should yield the formal argument.

When the constructor's done it has initialized T::value as a reference to a now
non-existing formal argument, yielding Undefined Behavior when that reference is
used  --  which for example means that two different compilers (or even separate
but identical invocations in same program) may produce different results.

Cheers & hth.,

- Alf


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Michael Tsang  
View profile   Translate to Translated (View Original)
 More options 9 Nov, 09:00
Newsgroups: comp.lang.c++
Follow-up To: comp.lang.c++
From: Michael Tsang <mikl...@gmail.com>
Date: Mon, 09 Nov 2009 17:00:18 +0800
Local: Mon 9 Nov 2009 09:00
Subject: Re: references and composition
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

"Nick Valeontis" <mouh...@frGGmail.gr[replace G to e]> wrote:

> Hmmm, i have been playing around with cpp, but, honestly, I need some help
> on this one...

You are writing C++, not cpp. CPP is a completely different language called
C Preprocessor.

value is *passed by value* to the constructor. In the initialiser, the class
member reference "this->value" is initialised by "value" parameter, which is
an object allocated on the stack of the initialiser. Therefore, this->value
is an object on the stack, which is deallocated as soon as the constructor
finishes. Now, this->value is an object which does not exist, any attempt to
access this->value is undefined behaviour.
> };

> int main(int argc, char** argv) {
>     Value v(5);
>     T t(v);

In this case, you passes the *value* stored inside v, not the object v
itself, because the parameter is passed by value.

>     cout << v.v;
>     cout << t.value.v;

>     //t.value.v++;        // output is 5555 !!?

t.value, as mentioned above, is an object which does not exist. You are
trying to access the sub-object t.value.v which lies inside an object which
does not exist, so undefined behaviour is invoked.

The difference here is "value" is passed by reference. Assuming you passes a
valid object to the constructor, "value" is *the object you've passed in*.
Then you again initialise this->value by reference, therefore, this->value
is the same object as value, also the same object you passed in. When the
constructor finished, this->value becomes the object you passed into the
constructor.
> };
> int main(int argc, char** argv) {
>     Value v(5);
>     T t(v);

In this case, you passed the *object* v into the constructor, because the
parameter is passed by reference. Therefore, the parameter "value" in t.T(v)
is the same object as v, when t.T(v) finishes running, t.value is the same
object as v.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAkr32jAACgkQG6NzcAXitM9XLQCff7yJESSNK1hjpDzJ73QGk0Os
kO8An1KfnWV6ztL6YPOhdtJWoYecUgrx
=aJ2r
-----END PGP SIGNATURE-----


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nick Valeontis  
View profile   Translate to Translated (View Original)
 More options 14 Nov, 23:52
Newsgroups: comp.lang.c++
From: "Nick Valeontis" <mouh...@frGGmail.gr[replace G to e]>
Date: Sun, 15 Nov 2009 01:52:28 +0200
Local: Sat 14 Nov 2009 23:52
Subject: Re: references and composition
hmm,

yep, i understand now what is going on.

I'd like to thank you both for the detailed answers!

:)

-Nick

"Michael Tsang" <mikl...@gmail.com> wrote in message

news:hd8lnf$bi0$1@news.eternal-september.org...


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
chad  
View profile   Translate to Translated (View Original)
 More options 15 Nov, 15:40
Newsgroups: comp.lang.c++
From: chad <cdal...@gmail.com>
Date: Sun, 15 Nov 2009 07:40:53 -0800 (PST)
Local: Sun 15 Nov 2009 15:40
Subject: Re: references and composition
On Nov 9, 1:00 am, Michael Tsang <mikl...@gmail.com> wrote:

Does the term *object* mean 'an instance of a class' in this case? Or
does the term *object* mean an unnamed variable?

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google