467,145 Members | 1,021 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,145 developers. It's quick & easy.

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
  • viewed: 1533
Share:
6 Replies

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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Kerry Neilson | last post: by
42 posts views Thread by Edward Diener | last post: by
8 posts views Thread by Francois | last post: by
2 posts views Thread by David | last post: by
5 posts views Thread by oliv@linuxmail.org | last post: by
34 posts views Thread by =?ISO-8859-1?Q?Marcel_M=FCller?= | last post: by
45 posts views Thread by =?Utf-8?B?QmV0aA==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.