where should this function defined?

T

Tagore

hi,
I have worked mainly in C. I started programming in C++ just 2
weeks ago.
supoose following classes
class A
{
... //some members
...
};
class B
{
...........
........... //other members
A a[10];
..........
};

Now while updating my project, I need a new function which should be
defined in class A....but it need to acces private members of class
B.....
what should I do?
 
A

AnonMail2005

hi,
    I have worked mainly in C. I started programming in C++ just 2
weeks ago.
supoose following classes
class A
{
        ...  //some members
        ...};

class B
{
         ...........
         ...........  //other members
          A a[10];
         ..........

};

Now while updating my project, I need a new function which should be
defined in class A....but it need to acces private members of class
B.....
what should I do?

There's not enough code to make a determination. There are ways to do
what you want to do (e.g. friend) if needed. But it's best to have
classes self contained - e.g. only they access their private data.
 
P

Paul N

hi,
    I have worked mainly in C. I started programming in C++ just 2
weeks ago.
supoose following classes
class A
{
        ...  //some members
        ...};

class B
{
         ...........
         ...........  //other members
          A a[10];
         ..........

};

Now while updating my project, I need a new function which should be
defined in class A....but it need to acces private members of class
B.....
what should I do?

You might get better help if you explain in a bit more detail what you
are attempting to do, as there are different approaches and which one
is "best" may depend on the situation.

One approach, especially if the function only needs to read (rather
than alter) B's private members, is to have a function in A and pass
in the necessary values as parameters to the function. Private members
could also be changed by passing in pointers or references.

Another approach is to see what A needs to do and write functions to
do that, then have the main routine in B which calls these.

A third possibility is for the classes to be friends. But have a
proper think before resorting to this.

Hope this helps.
Paul.
 

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,773
Messages
2,569,594
Members
45,121
Latest member
LowellMcGu
Top