Referencing values from an external class

I have certain counters in a class which need to be referenced from another class, in a separate source file. For example:

m_info_action[SmsAction].GetCount()

Is included in within a class which looks like:

BOOLEAN IdleScreen::OnMessage(const Message &msg)
{
...
...
...
}

How can I reference this value externally, i.e. find the value of GetCount, for SmsAction?

It's quite elementary, but I haven't touched C++ for quite some time. I'm using VC++ 6.0. Many thanks!
[549 byte] By [danny770] at [2007-11-19 9:00:18]
# 1 Re: Referencing values from an external class
It's a matter of placing them within the scope of the class that needs them.

If the counters are globally needed, then they can have a global scope. The best way to control global scope is with singletons.

If they are needed only locally and do not have a lifetime of the program, then pass the appropriate class in as a parameter.

This creates a dependency and increases coupling. If this increase is part of the implementation only and not part of the interface (i.e. the way the class is used externally) then use either forwardly-declared classes or use a base class/derived class model.
NMTop40 at 2007-11-9 0:44:07 >
# 2 Re: Referencing values from an external class
It's a matter of placing them within the scope of the class that needs them.

If the counters are globally needed, then they can have a global scope. The best way to control global scope is with singletons.

If they are needed only locally and do not have a lifetime of the program, then pass the appropriate class in as a parameter.

This creates a dependency and increases coupling. If this increase is part of the implementation only and not part of the interface (i.e. the way the class is used externally) then use either forwardly-declared classes or use a base class/derived class model.

The counters are used to calculate the amount of SMS received by a mobile phone.

"m_info_action[SmsAction].GetCount()" would allow me to get this figure if I were to call it within the original class/file, but I'm unsure how to reference it from outside, in literal code terms. It's not so much the theory of doing it, but rather the implementation, that I'm having problems with.
danny770 at 2007-11-9 0:45:02 >
# 3 Re: Referencing values from an external class
well have you done the design?

If you start off with the design (which is generally language independent) then you will usually find you can solve these situations in any language by your classes being defined properly
NMTop40 at 2007-11-9 0:46:11 >
# 4 Re: Referencing values from an external class
You may even have a static count member in ur class. which is incremented every time an sms is recieved. You can increment it at a location wherever and whenever u get to know u got a message.

As for the accessing of this member is concerned, you can access it by its class name directly as we do in case of static members provided its public.

If you wish to declare it as private/protected then in your GetCount method you could return this value.

(I assume you have the class header added wherever u want to access this member from)

Another way that can help you is - declaring the member in a file as "extern". try visiting this - http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/_langref_extern.asp

See what fits your scenario!

Keep ur cool... all the best.
Exterminator.
exterminator at 2007-11-9 0:47:09 >
# 5 Re: Referencing values from an external class
Sorry, I should have made it clearer... I'm designing a plug-in, and hence can't touch the original code. I simply have to reference that which is already there, but I'm not sure of the correct syntax for doing so. The variable is already in place, but I need to address it from another class/file.
danny770 at 2007-11-9 0:48:08 >
# 6 Re: Referencing values from an external class
of what type is m_info_action ? What is SmsAction?
NMTop40 at 2007-11-9 0:49:12 >
# 7 Re: Referencing values from an external class
Sorry, I should have made it clearer... I'm designing a plug-in, and hence can't touch the original code. I simply have to reference that which is already there, but I'm not sure of the correct syntax for doing so. The variable is already in place, but I need to address it from another class/file.

How has this variable been defined in the original existing code? Could you please share its implementation? Accessing a member depends a lot on that .. is it a member of a class? is it a static member of the class? is it a global variable? is it a file scope variable (global static)?what is it ? and then you can easily figure out the way to access it.

Cheers,
Exterminator
exterminator at 2007-11-9 0:50:08 >