Set and Get value

D

Donos

class CValue
{
public:
void set(int n1) {m_i[0] = n1;};
void get(int& n1) {n1 = m_i[0]};
protected:
int m_i[2];
};

class CSetValue
{
public:
void CSetValue::SendValue()
{
CValue m_Value;
int i = 10;
m_Value.set(i);
}
};

class CGetValue
{
public:
void CGetValue::RecieveValue()
{
int k;
CValue m_Value;
m_Value.get(k);
}
};

I am expecting in class CGetValue, the integer variable "k", should
get the value which was earlier set by "set" function.

But somehow this is not working.

Any idea?

Thanks
 
V

Victor Bazarov

Donos said:
class CValue
{
public:
void set(int n1) {m_i[0] = n1;};
void get(int& n1) {n1 = m_i[0]};

Remove the semicolons after the function bodies.
protected:
int m_i[2];
};

class CSetValue
{
public:
void CSetValue::SendValue()
{
CValue m_Value;
^^^^^^^^^^^^^^^
That's a local object. It exists only while this function is
executing.
int i = 10;
m_Value.set(i);
}
};

class CGetValue
{
public:
void CGetValue::RecieveValue()
{
int k;
CValue m_Value;
^^^^^^^^^^^^^^^
That's *another* local object. It exists only when this function
is executing. There is no connection between this object and
the object with the same name in 'CSetValue::SendValue'.
m_Value.get(k);
}
};

I am expecting in class CGetValue, the integer variable "k", should
get the value which was earlier set by "set" function.

But somehow this is not working.

Any idea?

Two unrelated objects in two unrelated functions. Setting the value
of one does not affect the value of the other.

You need to get out of OO (for now) and simply write

void foo() {
int a;
a = 5;
}

#include <iostream>

void bar() {
int a;
std::cout << a;
}

int main() {
foo();
bar();
}

Do you expect it to print out '5'? If yes, why? If not, why not?

V
 
J

Jim Langston

Donos said:
class CValue
{
public:
void set(int n1) {m_i[0] = n1;};
void get(int& n1) {n1 = m_i[0]};
protected:
int m_i[2];
};

class CSetValue
{
public:
void CSetValue::SendValue()
{
CValue m_Value;

m_Value is a local variable to SendValue.
int i = 10;

i is a local variable to SendValue
m_Value.set(i);

The local variable m_Value has it's set method called, so the local variable
m_Value has it's m_i[0] set to 10.

The function is over, so m_Value and i are destroyed. They don't exist
anymore.
};

class CGetValue
{
public:
void CGetValue::RecieveValue()
{
int k;

k is a local variable to the RecieveValue function
CValue m_Value;

m_Value is a local variable to the RecieveValue function. It is default
constructed (since the constructor is not initializing the array m_i it can
contain any values).
m_Value.get(k);

The local variable m_Value has it's get function called, which loads the
[any value] in m_i into k.

The function is over, m_Value and k are destroyed.
};

I am expecting in class CGetValue, the integer variable "k", should
get the value which was earlier set by "set" function.

To get the same varible that was set, you'll need to operate .get on the
same instance of the class as the .set.

Unless you want all isntances of CValue to have the same m_i array, in which
case declare it as static. Then there is just one m_i variable for all
instances of CValue.

Every time you do
CValue SomeVariableName;
you create a NEW instance of CValue with it's own m_i variable.
 
A

Andrej Hristoliubov

class CValue
{
public:
void set(int n1) {m_i[0] = n1;}
void get(int& n1) {n1 = m_i[0]}
protected:
int m_i[2];
};

class CSetValue
{
public:
void SendValue()
{
// CValue m_Value;
int i = 10;
m_Value.set(i);
}
};

class CGetValue
{
public:
void RecieveValue()
{
int k;
// CValue m_Value;
m_Value.get(k);
}
};

CValue m_Value;

void main()
{
CSetValue SetValue;
SetValue.SendValue();
CGetValue GetValue;
GetValue.RecieveValue();
}
 
O

Old Wolf

class CSetValue
{
public:
void SendValue()
{
// CValue m_Value;
int i = 10;
m_Value.set(i);

This is an error, the identifier m_Value is undeclared.

You have to put your declaration of m_Value before the
definition of CSetValue.
 

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,197
Messages
2,571,041
Members
47,643
Latest member
ashutoshjha_1101

Latest Threads

Top