Set() from one class and Get() from another

Dear all, I have some experience in c++; but in involved OOP, I am relatively inexperienced. I am trying to do the following:
I have two classes: A and B. In A, carry out some calculations and want to use them in B. It would be helpful if I could use Set/Get methods. For this, I wrote an intermediate class C.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
Header (.h)
class C
{
     public:
     C();
    ~C();
     
     void SetSomething(int n);
     int  GetSomething();
     
     int thing;
};

Source(.cxx)
C::C()
{
    thing = -999;
}
C::~C()
{
}
void SetSomething(int n)
{
   thing = n;
}
int GetSomething()
{
   return thing;
}


I can set the number from class A. First, I instantiate an object oA of C; and then I can set a number:
1
2
C *oA = new C();
oA->SetSomething(3);


But when I am trying to retrieve the number from class B, the following returns the default value initialized in the constructor. I am always getting N = -999.

1
2
C *oB = new C();
int N = oB->GetSomething(3);


I can vaguely understand this. When I am instantiating oB in class B, that time "thing" is getting overwritten with -999. Can anyone please suggest any technique so that this problem can be avoided? If it is not possible with Set/Get methods, what are the other possibilities?

Any help or hint is appreciated,
-regards
Kolahal

*** I am working in Scientific Linux.
Each time you call the new C() you create a new object. If you want to set and get the same value then you have to make sure that you call the set and get functions on the same object.
I don't really understand what you really want.

Could you explain more about your class A and B, or their some source?

I can't imagine what's relation between A,B and C.

I can see only object oA and oB created from C.


Is this what you want to do?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace std;

class A{
public :
    static int val;
    static void setVal(int x){ val = x; }
    int getVal(){ return val; }
};
int A::val = 0;

class B{
    public :
    void setVal(int x){A::setVal(x);}
};

int main(){
	A a;
	B b;
	b.setVal(10);
	cout << a.getVal();
}
Last edited on
¿did you know that if you close your eyes nobody can see you?


> C *oA = new C();
http://www.cplusplus.com/forum/general/138037/
Dear all, first of all, thanks for your replies. Let me clarify what I intend to do:

In the source directory of my code, among many other files, I have A.cxx and B.cxx (with headers in header file). There is no "friend"ship between them. In the file B.cxx, I wish to use a set of variables that have already been calculated in A.cxx.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
A header: (A.h)
class A
{
    public:
    ... ...
    ... ...
    
    private:
    
    bool      muzdir;
    int       nplane;
    double    chi_sqr;
    .... ....
};

A source (A.cxx)

#include "A.h"
#include <... ...>
#include <vector>

... ...

if (... ...)
muzdir = true;
else
muzdir =false;

... ...
for (... ...) { chi_sqr += ...}
... ...
etc.


I want to use these variables (muzdir, chi_sqr etc.) in B.cxx. First, I tried to declare these variables as public in A.h; then, including "A.h" among the dependencies, I tried to access them directly from B.cxx.
That did not work: compiler said that this variable was not declared in the scope.

Hence, I thought of Setting these numbers in an intermediate file "C": Set() from A, Get() from B. The class C was not declared as a "friend" class to either A or B. I tried to access the variables as described before. But that, as said before, is giving the value initialized in the constructor of C. Please let me know if I am clear. Thanks for your time,

-regards,
Kolahal
create a third class called Results. when your results are calculated in A populate a Results object with them, then pass this object into B, then you could have a method in B called update results:
1
2
  
void B::UpdateResults(Results& results);


you can embed the Results class in A and do something like:

1
2
3
4
5
6
7
8
9
A a;
// do stuff with a and calc results (which includes populating
// your internal Results class object).

B b;
b.updateResults(a.getResults());

// b is now up to date with all results from a.
Last edited on
I want to use these variables (muzdir, chi_sqr etc.) in B.cxx. First, I tried to declare these variables as public in A.h; then, including "A.h" among the dependencies, I tried to access them directly from B.cxx.
That did not work: compiler said that this variable was not declared in the scope.

It seems that the fundamental problem is in how you understand "class", "object", and "source file".

You clearly want an object with type B to read values from object that has type A. There is no immediate need for third party (C/Results).

Class is a description of type. Object is a variable that has some type and is thus an instance of type. You could have thousands of objects that all are of type A. Which one of them should your type B object use?
1
2
3
4
5
A a;
// do stuff with a
B b;
b.fill( a );
// do stuff with b 

The above requires:
B.h
1
2
3
4
5
6
class A;

class B {
public:
  void fill( const A & aa );
};

B.cpp
1
2
3
4
5
6
7
#include "B.h"
#include "A.h"

void B::fill( const A & aa )
{
  // read values from aa via its public interface
}
Last edited on
> but in involved OOP, I am relatively inexperienced.
(from the bluebook of smalltalk)
An object is a component of the system represented by some private memory and a set of operations.
A message is a request for an object to carry out one of its operations
A class is a description of a group of similar objects.
An instance is one of the objects described by a class


Let's say that you want to ask your colleague Alice about something.
You wouldn't just yell, nobody would know whom you are talking to.
You wouldn't ask Bob, that works in another company, or to Charlie and Dave that you just met on the street. Their context is different, so their answer would be useless.
You would simply call Alice and ask her a question.

So going back at your code.
It is not that "thing" is getting overwritten. What is happening is that `oA' and `oB' are different instances, their state is different and so is their response.

Let's say that you want to ask your colleague Alice about something.
You wouldn't just yell, nobody would know whom you are talking to.
You wouldn't ask Bob, that works in another company, or to Charlie and Dave that you just met on the street. Their context is different, so their answer would be useless.
You would simply call Alice and ask her a question.

So going back at your code.
It is not that "thing" is getting overwritten. What is happening is that `oA' and `oB' are different instances, their state is different and so is their response.


-Thanks ne555, I see the point. Thanks keskiverto and mutexe, I will try your suggestions and let you know when I get through this.
Topic archived. No new replies allowed.