| View previous topic :: View next topic |
| Author |
Message |
Amit Guest
|
Posted: Sat Aug 13, 2005 1:34 am Post subject: Single copy of a member across all instances of a class. |
|
|
Hi,
I was wondering if I have a class A, and a class B
what does the following mean:
class A{
class B &b;
....
}
in the header file?
Does this mean I will have one copy of b across all instantiations of
class A?
thanks,
--A.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Torsten Robitzki Guest
|
Posted: Sat Aug 13, 2005 1:14 pm Post subject: Re: Single copy of a member across all instances of a class. |
|
|
Amit wrote:
| Quote: | Hi,
I was wondering if I have a class A, and a class B
what does the following mean:
class A{
class B &b;
...
}
in the header file?
Does this mean I will have one copy of b across all instantiations of
class A?
|
No, it's simply a per object of type A reference to an instance of an
Object of type B. The keyword class is superflusious here and the same
line can be writter as:
B& b;
regards
Torsten
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Mike Capp Guest
|
Posted: Sat Aug 13, 2005 1:16 pm Post subject: Re: Single copy of a member across all instances of a class. |
|
|
Amit wrote (edited):
| Quote: | what does the following mean:
class A { class B &b; };
|
It's a normal instance member with a forward declaration rolled in. In
other words, it's roughly equivalent to the more usual:
class B;
class A { B& b; };
cheers
Mike
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Rob Guest
|
Posted: Sat Aug 13, 2005 2:52 pm Post subject: Re: Single copy of a member across all instances of a class. |
|
|
Amit wrote:
| Quote: | Hi,
I was wondering if I have a class A, and a class B
what does the following mean:
class A{
class B &b;
...
}
in the header file?
Does this mean I will have one copy of b across all instantiations of
class A?
|
No.
The declaration actually does two things. The first is a forward
declaration of class B. The second is that each instance of A will
contain a member named b, which is a reference to an instance of class
B.
Presumably the constructors of A initialise the member b so it refers
to an instance of class B (or a class derived from B).
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Maeder Guest
|
Posted: Sat Aug 13, 2005 2:53 pm Post subject: Re: Single copy of a member across all instances of a class. |
|
|
"Amit" <amit.bhatia (AT) gmail (DOT) com> writes:
| Quote: | I was wondering if I have a class A, and a class B
what does the following mean:
class A{
class B &b;
|
[side note: this would more commonly be written
B &b;
, i.e. without the keyword class.]
| Quote: | ...
}
in the header file?
Does this mean I will have one copy of b across all instantiations
of class A?
|
No.
It means that each instance of class A references an instance of class
B. Whether all instances of A reference the same instance of class B
or not depends on how the member b is initialized; it is possible, but
we can't deduce it from the code you posted.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Carlos Moreno Guest
|
Posted: Sun Aug 14, 2005 3:58 am Post subject: Re: Single copy of a member across all instances of a class. |
|
|
Torsten Robitzki wrote:
| Quote: | what does the following mean:
class A{
class B &b;
...
}
in the header file?
Does this mean I will have one copy of b across all instantiations of
class A?
No, it's simply a per object of type A reference to an instance of an
Object of type B. The keyword class is superflusious here and the same
line can be writter as:
B& b;
|
No it's not. (the way the code is written/shown, assuming that that
is all about classes A and B, then the keyword class is required for
the above to compile).
If you omit the keyword class in there, you would need a forward
declaration for class B.
Carlos
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Valentin Kreh Guest
|
Posted: Mon Aug 15, 2005 10:53 am Post subject: Re: Single copy of a member across all instances of a class. |
|
|
Amit schrieb:
| Quote: | Hi,
I was wondering if I have a class A, and a class B
what does the following mean:
class A{
class B &b;
This is a reference to an object of class B. That means instead of |
keeping a complete object of class B, you only keep the adress of an
allready existing object. You could of course give all instances of
class A the same reference B &b, so that all A objects work with the
same copy of b:
class B;
class A{
public:
A(B &b_in); // constructor
private:
B &b;
}
A::A(B &b_in):
b(b_in) // initialize b
{
}
main()
{
B my_only_b;
A a1(my_only_b);
A a2(my_only_b);
A a3(my_only_b);
}
But this is not the right way to do this in c++ and there is no way to
prevent someone from declaring
B some_other_b;
A a4(some_other_b);
If you want to have one copy of a member variable for all
instantiations, the right way to do this in c++ is to declare it static:
class B;
class A{
public:
A(); // constructor
private:
static B b; // one copy for all instances
}
Valentin
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|