Using singleton class instance in another class

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi, I got a singleton class. Then in another class (CA), i would declare a
variable with this singleton class and get a reference to it. I would have
another class (IA1) that would inherit from CA and I would want to allow IA1
to access the singleton class. My question is is it better to declare the
singleton class as private or protected in CA? Or shall I let IA1 to get a
reference to the singleton by itself?

Thanks.
Eugene
 
Hi Eugene,
make the variable protected so that the base and derived class use the
same member variable to reference the Singleton. One benefit of inheritance
is to allow code reuse from the base class.

Mark R Dawson
http://www.markdawson.org
 
Mark R. Dawson said:
make the variable protected so that the base and derived class use the
same member variable to reference the Singleton. One benefit of inheritance
is to allow code reuse from the base class.

Alternatively (and preferrably, IMO) provide a protected *property*
rather than a protected variable. If you use the property everywhere in
both classes, that gives you more flexibility if you later change from
the singleton to a factory, for instance.
 

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

Back
Top