implement association in C++

R

roadster

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....
 
V

valentin tihomirov

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.
 
R

Rolf Magnus

roadster said:
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();
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,229
Latest member
GloryAngul

Latest Threads

Top