Connecting Tech Pros Worldwide Forums | Help | Site Map

Passing Private Reference to Object

hits
Guest
 
Posts: n/a
#1: Jul 22 '05
Should the following passing of private variable refrence work?
( Relates to the power of references to access private data variable).
class B;

class A
{
private:
int var;
void func_a()
{
B b;
b.func_b(var);
}
};

class B
{
func_b(int &a)
{
a=a+1;
}
};

Karl Heinz Buchegger
Guest
 
Posts: n/a
#2: Jul 22 '05

re: Passing Private Reference to Object


hits wrote:[color=blue]
>
> Should the following passing of private variable refrence work?
> ( Relates to the power of references to access private data variable).
> class B;
>
> class A
> {
> private:
> int var;
> void func_a()
> {
> B b;
> b.func_b(var);
> }
> };
>
> class B
> {
> func_b(int &a)
> {
> a=a+1;
> }
> };[/color]

Yes. It should work. 'var' is a private member of A. Thus
an A object can do anything with it it wants. And that of
course includes passing it to other objects.

--
Karl Heinz Buchegger
kbuchegg@gascad.at
Alf P. Steinbach
Guest
 
Posts: n/a
#3: Jul 22 '05

re: Passing Private Reference to Object


* hits:[color=blue]
>
> Should the following passing of private variable refrence work?[/color]

Yes, but the rest of the code should not compile.

[color=blue]
> ( Relates to the power of references to access private data variable).
> class B;
>
> class A
> {
> private:
> int var;
> void func_a()
> {
> B b;[/color]

B cannot be used as type for a variable because the definition of class B
has not yet been encountered.

[color=blue]
> b.func_b(var);
> }
> };
>
> class B
> {
> func_b(int &a)
> {
> a=a+1;
> }
> };[/color]

Style: do use consistent non-zero indentation.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Closed Thread