Assigning value from one class to another

Discussion in 'C++' started by Donos, Oct 10, 2007.

  1. Donos

    Donos Guest

    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.
    Donos, Oct 10, 2007
    #1
    1. Advertising

  2. On Oct 10, 5:44 pm, Donos <> wrote:
    > 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).
    tragomaskhalos, Oct 10, 2007
    #2
    1. Advertising

  3. Donos

    Donos Guest

    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.
    Donos, Oct 10, 2007
    #3
  4. Donos

    red floyd Guest

    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.
    red floyd, Oct 10, 2007
    #4
  5. Donos

    Daniel T. Guest

    Donos <> wrote:

    > 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.
    Daniel T., Oct 10, 2007
    #5
  6. Donos

    Donos Guest

    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;
    }
    Donos, Oct 10, 2007
    #6
  7. Donos

    Daniel T. Guest

    Donos <> wrote:

    > 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.
    Daniel T., Oct 11, 2007
    #7
  8. Donos

    Daniel T. Guest

    Donos <> wrote:

    > 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.
    Daniel T., Oct 11, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. loveNUNO
    Replies:
    2
    Views:
    891
    loveNUNO
    Nov 20, 2003
  2. marekw2143
    Replies:
    3
    Views:
    1,348
    marekw2143
    Jul 25, 2009
  3. Ted
    Replies:
    0
    Views:
    123
  4. Robert Cohen
    Replies:
    3
    Views:
    248
    Andrew Durstewitz
    Jul 15, 2003
  5. weston
    Replies:
    1
    Views:
    238
    Richard Cornford
    Sep 22, 2006
Loading...

Share This Page