473,289 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

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

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 '05 #1
6 1802

amit.bha...@gmail.com 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

Aug 12 '05 #2
am*********@gmail.com 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
Aug 12 '05 #3
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

Aug 12 '05 #4
>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,
zo****@ZHMicro.com
http://www.zhmicro.com
http://distributed-software.blogspot.com
http://groups-beta.google.com/group/...Z?lnk=la&hl=en

Aug 13 '05 #5
Victor , We know the compiler will accept this:

class B
{

};

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

Aug 13 '05 #6
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
Aug 13 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Kerry Neilson | last post by:
Hi, Really hung up on this one. I'm trying to get all the fields of a dictionary to be unique for each class: class A { my_dict = dict_entry = { 'key1':0, 'key2':0 } __init__(self): for...
42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
15
by: Mon | last post by:
I am in the process of reorganizing my code and came across and I came across a problem, as described in the subject line of this posting. I have many classes that have instances of other classes...
8
by: SJ | last post by:
Hi: I have a class which has a static member function. The function implements something common to all instances. How can the static member function know all of the (Get access to the instances'...
8
by: Francois | last post by:
Hi all, Each time an HTTP request is received by IIS, it forwards it to the ASP.NET worker process. Then does the ASP.NET worker process in turn start a new thread for each HTTP request it...
2
by: David | last post by:
How do I share code across files in C#, without including a reference to a class library. Here's what I want to do. I have a device I want to talk to. It's a machine in a factory. It can talk...
5
by: oliv | last post by:
Hi, New to .NET, I was wondering what was the proper way to share a variable between all the instances of a web page. I try with a static var, but it does not seem to always work. Why is that ? ...
34
by: =?ISO-8859-1?Q?Marcel_M=FCller?= | last post by:
Hi, is there a way to avoid the automatic copy constructor generation. I do not want the object to be non-copyable. I simply do not want that copying is done by the default copy constructor. But...
45
by: =?Utf-8?B?QmV0aA==?= | last post by:
Hello. I'm trying to find another way to share an instance of an object with other classes. I started by passing the instance to the other class's constructor, like this: Friend Class...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.