Passing Private Reference to Object

H

hits

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;
}
};
 
K

Karl Heinz Buchegger

hits said:
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;
}
};

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

Alf P. Steinbach

* hits:
Should the following passing of private variable refrence work?

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

( Relates to the power of references to access private data variable).
class B;

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

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

b.func_b(var);
}
};

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

Style: do use consistent non-zero indentation.
 

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

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,479
Latest member
JaysonK723

Latest Threads

Top