Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Sharing same copy of a member across all instances of a class.

Reply
Thread Tools

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

 
 
amit.bhatia@gmail.com
Guest
Posts: n/a
 
      08-12-2005
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.

 
Reply With Quote
 
 
 
 
Dan Cernat
Guest
Posts: n/a
 
      08-12-2005

(E-Mail Removed) 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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-12-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-12-2005
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
>

 
Reply With Quote
 
Zorro
Guest
Posts: n/a
 
      08-13-2005
>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,
(E-Mail Removed)
http://www.zhmicro.com
http://distributed-software.blogspot.com
http://groups-beta.google.com/group/...Z?lnk=la&hl=en

 
Reply With Quote
 
Frank Chang
Guest
Posts: n/a
 
      08-13-2005
Victor , We know the compiler will accept this:

class B
{

};

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

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-13-2005
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Searching all instances of a pattern across multi-lines laredotornado Perl Misc 3 12-16-2009 08:29 PM
Need same viewstate for same page across multiple servers =?Utf-8?B?UHVuaXNoZXI=?= ASP .Net 1 03-23-2006 03:54 PM
How can a Static member function know all instances? SJ C++ 8 05-21-2004 02:38 AM
list of class instances within a list of a class instances John Wohlbier Python 2 02-22-2004 08:41 AM
2 Browser Instances sharing the same session (using Ctrl-N) =?Utf-8?B?QXVndXN0aW4gUHJhc2FubmEuIEo=?= ASP .Net 1 02-02-2004 02:23 AM



Advertisments