implement association in C++

Discussion in 'C++' started by roadster, Jun 16, 2004.

  1. roadster

    roadster Guest

    Hi,

    I'm new to C++ and I don't know how I can implement an association
    between two objects.

    e.g. we have objects A and B. B has the following interface-function :
    GetInfo()

    From within a function of A I want to get info of object B : so I need
    to call :
    B->GetInfo();

    This gives a compile error, saying that B is an unkown variable. I can
    solve this by making B a global class, but I know that this isn't the
    right way to do this.

    How do I need to do this? Any examples?


    Greetz....
     
    roadster, Jun 16, 2004
    #1
    1. Advertising

  2. Map associates element from one set with elements of another set. It allows
    for is 1-to-N association. If you want 1-to-1, you can join the sub-objects
    into one class.
     
    valentin tihomirov, Jun 16, 2004
    #2
    1. Advertising

  3. roadster

    Rolf Magnus Guest

    roadster wrote:

    > Hi,
    >
    > I'm new to C++ and I don't know how I can implement an association
    > between two objects.
    >
    > e.g. we have objects A and B. B has the following interface-function :
    > GetInfo()
    >
    > From within a function of A I want to get info of object B : so I need
    > to call :
    > B->GetInfo();
    >
    > This gives a compile error, saying that B is an unkown variable.


    From your description above, B is a class, not an object. You need an
    instance of your class to call the GetInfo function on.

    > I can solve this by making B a global class,


    You probably mean a global object.

    > but I know that this isn't the right way to do this.
    >
    > How do I need to do this? Any examples?


    Well, first you need to get an object of class B. If your A is supposed
    to contain a B, it might look something like:

    //... definition of class B

    class A
    {
    public:
    void doSomething();
    private:
    B b;
    };

    void A::doSomething()
    {
    b.GetInfo();
    }
     
    Rolf Magnus, Jun 16, 2004
    #3
    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. MK
    Replies:
    2
    Views:
    2,518
  2. Mike
    Replies:
    6
    Views:
    1,178
  3. Alex Rast
    Replies:
    2
    Views:
    1,084
    Alex Rast
    Nov 10, 2004
  4. sonic
    Replies:
    1
    Views:
    553
    John Saunders
    Jan 7, 2005
  5. newsnet customer
    Replies:
    5
    Views:
    30,016
    Chris Uppal
    Nov 9, 2006
Loading...

Share This Page