Sharing same copy of a member across all instances of a class.

Discussion in 'C++' started by amit.bhatia@gmail.com, Aug 12, 2005.

  1. Guest

    Hi,
    I have also posted this to the moderated group. I have 2 classes A and
    B,
    what does the following mean in header file for class A:

    class A
    {
    class B &b;
    ....
    };

    Does this imply that the copy is being shared across all instances of
    class A?
    I know we use static, but I have come across this in a code and am not
    sure what this means..

    thanks,
    --A.
    , Aug 12, 2005
    #1
    1. Advertising

  2. Dan Cernat Guest

    wrote:
    > Hi,
    > I have also posted this to the moderated group. I have 2 classes A and
    > B,
    > what does the following mean in header file for class A:
    >
    > class A
    > {
    > class B &b;
    > ...
    > };
    >

    an object of type A holds a reference to an object of type B. Think of
    it as a pointer which cannot be reassigned and has to be initialized
    during the construction of the object of type A

    > Does this imply that the copy is being shared across all instances of
    > class A?


    no, it is per object

    > I know we use static, but I have come across this in a code and am not
    > sure what this means..
    >
    > thanks,
    > --A.


    example:

    class B
    {
    };

    class A
    {
    B& b;
    public:
    A(const B& refB):b(refB)
    {}
    };

    int main()
    {
    B b1, b2, b3;
    A a1(b1), a2(b1), a3(b2), a4(b3);
    // above, b1 will be shared by a1 and a2

    return 0;
    }

    dan
    Dan Cernat, Aug 12, 2005
    #2
    1. Advertising

  3. wrote:
    > I have also posted this to the moderated group. I have 2 classes A and
    > B,
    > what does the following mean in header file for class A:
    >
    > class A
    > {
    > class B &b;
    > ...
    > };
    >
    > Does this imply that the copy is being shared across all instances of
    > class A?


    No.

    > I know we use static, but I have come across this in a code and am not
    > sure what this means..


    It means that an instance of A does not _own_ another instance of B, but
    only _refers_ to it. A pre-condition for creating an instance of A is
    that an instance of B should already exist.

    V
    Victor Bazarov, Aug 12, 2005
    #3
  4. Dan Cernat wrote:
    > [...]
    > class B
    > {
    > };
    >
    > class A
    > {
    > B& b;
    > public:
    > A(const B& refB):b(refB)


    Error: cannot convert a reference to const to a reference to non-const.

    > {}
    > };
    >
    > int main()
    > {
    > B b1, b2, b3;
    > A a1(b1), a2(b1), a3(b2), a4(b3);
    > // above, b1 will be shared by a1 and a2
    >
    > return 0;
    > }
    >
    > dan
    >
    Victor Bazarov, Aug 12, 2005
    #4
  5. Zorro Guest

    >Hi,
    > I have also posted this to the moderated group. I have 2 classes A >and
    >B,
    >what does the following mean in header file for class A:



    >class A
    >{
    > class B &b;
    >...




    >};



    >Does this imply that the copy is being shared across all instances of
    >class A?
    >I know we use static, but I have come across this in a code and am >not
    >sure what this means..


    >thanks,
    >--A.



    Replying to Amit (the person asking the question).

    Your question has been answered quite professionally in accordance to
    the standard. My comment is on a different issue.

    The notion of reference is not related to type. In simplest terms, it
    creates an alias (another name) for an existing OBJECT. For instance,
    in pass by reference, or in the following declaration:

    int & r = n;

    The identifier r is another name for the object named n.

    When defining a class, we are introducing a new type using existing
    types. The names of members of class are names of objects (of specified
    types). It will take me too long to logically explain that a pointer is
    a type (not a type modifier). At any rate, pointer types are needed for
    recursive structures like linked lists.

    Now, what in the world does it mean for the name of a member of a class
    to be an alias for another object? We are defining a type, why does it
    have to depend on the existence of an OBJECT? The game played by the
    compiler, to wait until we create an instance of our class, and then
    require the existence of an object, can be done to accommodate for
    anything we want. Except, programming is too hard to be confused with a
    game.

    I really think now that you know how to read other people's code do
    not be encouraged to use it.

    Regards,

    http://www.zhmicro.com
    http://distributed-software.blogspot.com
    http://groups-beta.google.com/group/computerlangZ?lnk=la&hl=en
    Zorro, Aug 13, 2005
    #5
  6. Frank Chang Guest

    Victor , We know the compiler will accept this:

    class B
    {

    };

    class A
    {
    B& b;
    public:
    A(const B& refB):b(const_cast<B&>(refB))
    {}
    };
    Frank Chang, Aug 13, 2005
    #6
  7. Frank Chang wrote:
    > Victor , We know the compiler will accept this:
    >
    > class B
    > {
    >
    > };
    >
    > class A
    > {
    > B& b;
    > public:
    > A(const B& refB):b(const_cast<B&>(refB))
    > {}
    > };



    The compiler will accept this too:

    int main() {
    int *pint = 0;
    *pint = 42;
    }

    It doesn't mean one should do that.

    V
    Victor Bazarov, Aug 13, 2005
    #7
    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. =?Utf-8?B?QXVndXN0aW4gUHJhc2FubmEuIEo=?=

    2 Browser Instances sharing the same session (using Ctrl-N)

    =?Utf-8?B?QXVndXN0aW4gUHJhc2FubmEuIEo=?=, Jan 31, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,255
    Craig Deelsnyder
    Feb 2, 2004
  2. Replies:
    0
    Views:
    403
  3. John Wohlbier
    Replies:
    2
    Views:
    358
    Josiah Carlson
    Feb 22, 2004
  4. laredotornado
    Replies:
    3
    Views:
    96
  5. ardi
    Replies:
    2
    Views:
    201
Loading...

Share This Page