Assigning value from one class to another

D

Donos

Hello

I posted this earlier,but some how it's still not working for me. So i
thought of giving a better description this time.

The following is the code that am using,

class CMyValue
{
public:
CCmdValue m_pValue;

unsigned char* pBuf = NULL;
m_pValue.GetValue(pBuf); // Expecting return for pBuf here
};

class CCmdValue
{
public:
void CCmdValue::GetValue(unsigned char*& pVal)
{
pVal = NewValue(); // This function returns a
unsigned char*
}
};

Now am expecting the class CMyValue to get the return value of
function "NewValue()" in class CCmdValue, through the unsigned char*
pVal.

But this is not working.
I tried this in many different ways, but still fails.

Any help is welcome.

Thanks.
 
T

tragomaskhalos

Hello

I posted this earlier,but some how it's still not working for me. So i
thought of giving a better description this time.

The following is the code that am using,

class CMyValue
{
public:
CCmdValue m_pValue;

unsigned char* pBuf = NULL;
m_pValue.GetValue(pBuf); // Expecting return for pBuf here
};

As was noted in the other thread, this won't
compile - you can't put statements in a class
body. I *assume* you want to set the value
at construction time, in which case you can
do this:

class CMyValue
{
public:
CCmdValue m_pValue;
unsigned char* pBuf;
CMyValue() { m_pValue.GetValue(pBuf);}
};

BTW sticking C- on the front of all
classes is an MFC convention, it's a bit
bogus doing it in one's own code (though
many do).
 
D

Donos

Please understand that the code given is just a sample. It's just to
give an idea that there are couple of classes involved in this
scenario.
 
D

Daniel T.

Donos said:
Hello

I posted this earlier,but some how it's still not working for me. So i
thought of giving a better description this time.

The following is the code that am using,

class CMyValue
{
public:
CCmdValue m_pValue;

unsigned char* pBuf = NULL;
m_pValue.GetValue(pBuf); // Expecting return for pBuf here
};

class CCmdValue
{
public:
void CCmdValue::GetValue(unsigned char*& pVal)
{
pVal = NewValue(); // This function returns a
unsigned char*
}
};

Now am expecting the class CMyValue to get the return value of
function "NewValue()" in class CCmdValue, through the unsigned char*
pVal.

But this is not working.
I tried this in many different ways, but still fails.

Any help is welcome.

How is "NewValue()" defined? If it is returning a pointer to something
that falls out of scope, that would be a huge problem.
 
D

Donos

NewValue() function is defined as,

unsigned char* CClassName::NewValue()
{
unsigned char* pChar;
BYTE command[] = {ONE_CMD, TWO_CMD, THREE_CMD}; // These are macros
pChar = command;
return pChar;
}
 
D

Daniel T.

Donos said:
NewValue() function is defined as,

unsigned char* CClassName::NewValue()
{
unsigned char* pChar;
BYTE command[] = {ONE_CMD, TWO_CMD, THREE_CMD}; // These are macros
pChar = command;
return pChar;
}

Eventually you will learn... How are the macros defined? Give us
something we can compile and test and we can help you.
 
D

Daniel T.

Donos said:
NewValue() function is defined as,

unsigned char* CClassName::NewValue()
{
unsigned char* pChar;
BYTE command[] = {ONE_CMD, TWO_CMD, THREE_CMD}; // These are macros
pChar = command;
return pChar;
}

There is your problem. "command" looses scope when the function exits,
the pointer you are returning is invalid.
 

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

Staff online

Members online

Forum statistics

Threads
473,775
Messages
2,569,601
Members
45,182
Latest member
BettinaPol

Latest Threads

Top