Archived

This topic is now archived and is closed to further replies.

How do you make 2 class instances synchronize with eachother?

This topic is 4950 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Say if you have a class, CSomething, and you declare a global instance of this class: CSomething someClass; And then you have another class called CSomethingElse, which is defined as: class CSomethingElse { public: CSomething m_Class; }; Then you create an instance of the CSomethingElse class, how could you make the global ''someClass'', and the public member ''m_Class'' equal to each other? and so that every thing you do to the global someClass, also happens to m_Class?? [~aussie]

Share this post


Link to post
Share on other sites
Use a pointer inside the CSomethingElse, instead of an actual embedded class. Set the pointer to point at the global instance.

BTW, "synchronization" is a completely different thing.

Share this post


Link to post
Share on other sites
Declare m_Class as static. A static data member is like a global variable that has access restricted by the member access rules of the class. A static data member exists seperately from instances of the class, and can be refered to without a class instance.

class CSomethingElse
{
public:
static CSomething m_Class;
};

// static members need to be initialized once in the file scope like a global
CSomething CSomethingElse::m_Class();

// static members can be accessed via a class instance...
CSomethingElse foo;

foo.m_Class.member();

// or without an instance
CSomethingElse::m_Class.member();

Share this post


Link to post
Share on other sites
Take that approach (jsh''s) if you want a CSomething which is common to all CSomethingElse''s.

(As an added benefit, you can put it in the private part of the class, and make the global-ness less smelly, by restricting scope to just what''s needed. I''m assuming here that you won''t need the CSomething outside the CSomethingElse instances, here, though.)

Take my approach if you want a really global CSomething, which some CSomethingElse''s use (but not necessarily all).

Share this post


Link to post
Share on other sites