CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Feb 2011
    Posts
    32

    Question Simple classes question

    I have 3 class in 3 files
    Code:
    //file1.h
    typedef struct 
    {
        int a;
        double b;
    } A;
    
    class B
    {
       public:
           A*pA;
           void somefunc(int a){ pA->a=a;}
    };
    
    
    //file2.h
    
    class C
    {
    public:
       B*pB;
       void somefunc(int b){pB->pA->b=b;}
    };
    
    
    //file 3
    #include "file1.h"
    #include "file2.h"
    #include < iostream>
    using namespace std;
    int main()
    {
       B rB;
       rB.somefunc(1);
       C rC;
       rC.somefunc(2);
       
       cout<<rC.pB->pA->a<<rC.pB->pA->b;
    }
    But this fails at cout unexpectedly

    Someone offer me a helping hand please/////

    Thank you

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Simple classes question

    What do you think pA is pointing to in your B instance?

  3. #3
    Join Date
    Feb 2011
    Posts
    32

    Re: Simple classes question

    I change it into reference but still it doesn't work

  4. #4
    Join Date
    Aug 2008
    Posts
    902

    Re: Simple classes question

    The use of whitespace would make your code more readable.

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Simple classes question

    Quote Originally Posted by Lucassergei View Post
    I change it into reference but still it doesn't work
    I wouldn't expect it to. References and pointers refer or point to other objects.

    Why not just make it an instance?

    Code:
    A m_A;
    And I agree, please get in the habit of using spaces.

  6. #6
    Join Date
    Feb 2005
    Location
    Denver
    Posts
    353

    Re: Simple classes question

    I agree with GCDEF, it would be much easier to just make it an instance. If you insist on using pointers, you'll have to allocate them. You'll do this in your constructors (which you have neglected to implement). You'll also have to deallocate these pointers, which you'll do in your destructors (which you also must implement).

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Simple classes question

    Either it needs to be an actual object, or you need to direct the pointer at some existing object before trying to use it.

  8. #8
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: Simple classes question

    You don't need typedef keyword in defining structs in C++. You have pointers to A and B but never have those been initialized before deferencing them. Of course, it will lead to a crash.

    Also, if I may comment on the purpose being served by your code. You have a structure which is being operated upon by B (which only sets one of the members) and C (which sets the other member, and with extra level of nesting i.e. via a pointer to an object of type B, and B is just setting the member directly of that of object of A). What purpose does it serve? Why not have just one class A and have the parameterized constructors that setup values for both members of it. But may be you have a bigger purpose than this but if that bigger purpose does try achieve the same as you have depicted by the above sample code, it seems very inelegant.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured