Thread: Data member passing

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    22

    Data member passing

    Hi there,

    Could somebody please guide me with the procedure to pass the value of:

    a) A data member, defined in one class, into another class
    b) A data member, defined in a function of a class, into another function of the same class

    Would gr8ly appreciate advise here.

    Best,
    wirefree101

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    I would say that depends on about a zillion different things. What exactly are you trying to achieve here? have you got an example of why you want to pass data around like that?

    Most commonly, passing individual data members around from one class to another is a symptom of bad design (Not always, but alot can start to go wrong when a member function begins directly reading & modifying members of a different class)

    There's almost certainly an alternative approach - if you give us details of what you're trying to do, someone may be able to suggest something.

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    >A data member, defined in one class, into another class

    Interherence. You can define a data member as protected in a form of a 'base' class and use it in another class.
    Double Helix STL

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Interherence.
    Err... do you mean "inheritance"?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Yes, sorry it was late when i typed that and I didnt notice.
    Double Helix STL

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    22
    Appreciate the responses.

    Following are the specifics:

    - IDE generates 5 errors related, essentially, with the non-transferability/usage of:

    a) ‘bLocStr’ (defined in WrUAppUi.cpp) being referred to in MTMSExampleEngine.cpp.

    defined here...
    Code:
    File: WrUAppUi.cpp
    
    CWrUAppUi::HandleSessionEventL(TMsvSessionEvent aEvent, TAny* aArg1, TAny* aArg2, TAny* /*aArg3*/)
        {
        switch (aEvent)
    ...
    ...
    TBuf16<128> bLocStr;
    ...
    used here...
    Code:
    File: CMtmsExampleEngine.cpp
    
    void CMtmsExampleEngine::CreateDraftSMSL(const TDesC& aAddress)
        {
    ...
    ...
    body.InsertL(0, bLocStr);
    ...

    b) ‘address’ (defined, & used, earlier in the same class) being referred to in HandleSessionEventL() of WrUAppUi.cpp

    defined here...
    Code:
    File: WrUAppUi.cpp
    
    void CWrUAppUi::HandleCommandL(TInt aCommand)
        {
    ...
    ...
    TBuf<30> address;
    GetNumber(address);
    iEngine->CreateDraftSMSL(address);
    ...
    used here...
    Code:
    File: WrUAppUi.cpp
    
    void CWrUAppUi::HandleSessionEventL(TMsvSessionEvent aEvent, TAny* aArg1, TAny* aArg2, TAny* /*aArg3*/)
        {
        switch (aEvent)
    ...
    ...
    iEngine->CreateDraftNewSMSL(address);
    ...
    Would appreciate a prompt response.


    Best,
    wirefree101

  7. #7
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You cannot access local variables of one (member) function in another function.
    Make them class members or pass them as parameters.
    Kurt

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    22
    Thx for the response.

    Issue (b) has resolved by declaring 'address' as 'public' in WrUAppUi.h.

    However, with reference to issue (a) above, can you please advise me on how to pass value to other classes and use them there. (Note: TBuf16<128> bLocStr is defined in WruAppUi.cpp & assigned a value there. Now it needs to be used in CMtmsExampleEngine.cpp)

    Thx in advance,
    wirefree101

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. data structure design for data aggregation
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-20-2008, 06:43 AM
  2. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-08-2007, 12:24 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. typecasting data for parameter passing
    By daluu in forum C++ Programming
    Replies: 8
    Last Post: 12-08-2004, 02:58 PM
  5. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM