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

A

amit.bhatia

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.
 
D

Dan Cernat

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
 
V

Victor Bazarov

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
 
V

Victor Bazarov

Dan said:
[...]
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.
 
Z

Zorro

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 address removed)
http://www.zhmicro.com
http://distributed-software.blogspot.com
http://groups-beta.google.com/group/computerlangZ?lnk=la&hl=en
 
F

Frank Chang

Victor , We know the compiler will accept this:

class B
{

};

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

Victor Bazarov

Frank said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top