473,471 Members | 1,695 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Assigning value from one class to another

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.

Oct 10 '07 #1
7 1519
On Oct 10, 5:44 pm, Donos <dongu...@gmail.comwrote:
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).

Oct 10 '07 #2
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.

Oct 10 '07 #3
Donos wrote:
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.
And if you read the FAQ
(http://www.parashift.com/c++-faq-lite/how-to-post.html) and the
responses, you'll see that we can't help you until you post a minimal,
compilable example that exhibits the behavior in question. Your
"sample" isn't anywhere near good enough.
Oct 10 '07 #4
Donos <do******@gmail.comwrote:
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.
Oct 10 '07 #5
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;
}
Oct 10 '07 #6
Donos <do******@gmail.comwrote:
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.
Oct 11 '07 #7
Donos <do******@gmail.comwrote:
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.
Oct 11 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Ricardo Sta. Rita | last post by:
Hello people, We have been dealing with for a long time now. It's a bit weird for a newcomer like us so we decided to ask the geniuses here. C# doesn't seem to be assigning values, consider...
9
by: Christopher Weaver | last post by:
Can anyone tell me how I could iterate through a collection of controls on a form while assigning their event handlers to another identical collection of controls on the same form. So far,...
4
by: Jon | last post by:
This seems strange, but maybe there's some basic concept I'm missing. When I assign one class member to another, any methods that are applied to one are applied to both variables.I can't get the...
0
by: ryoung | last post by:
I receive the following error when I attempt to assign values to a web service array. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. ...
25
by: Sourav | last post by:
Suppose I have a code like this, #include <stdio.h> int *p; void foo(int); int main(void){ foo(3); printf("%p %d\n",p,*p);
3
by: Froefel | last post by:
Hi group I am creating a web application that uses a simple DAL as an ObjectDataSource. To retrieve data from the database, I use a DataReader object from which I then assign the various fields...
8
by: getmeidea | last post by:
Hi, I am using JDK 1.5. I have a program like this. Here i am directly assigning value to one object. It does'nt give me any compile time or run time error. In java we dont have access to any...
8
by: =?Utf-8?B?VHJlY2l1cw==?= | last post by:
Hello, Newsgroupians: I have a large class with a lot of member variables. I also have a function in the class that I would like to change ALL Of the member variables. I am trying to assign...
1
by: The Pythonista | last post by:
I've been wondering for a while about whether assigning to __class__ is bad form or not. Specifically, I mean doing so when some other method of implementing the functionality you're after is...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.