On Tue, 15 May 2007 10:47:52 +0200, anon wrote:
jeff_j_dunlap@yahoo.com wrote:
Whenever I need class wide access to an object, I declare it
What is "class wide access to an object"?
dynamically:
class myClass
{
...
myObject* obj; // declared dynamically ...
Then I usually create an instance of the object within the constructor:
myClass::myClass()
{
...
string str = "whatever";
obj = new myObj(str); // parameter passed to obj ...
Doing this, I am able to access myObj::obj anywhere from within my
What is myObj?
class. Is it possible to declare obj as a static member variable? If
so, I have not been able to figure out how to do it.
I do not see why not. You can do it like this: class myClass
{
public:
static myObject obj;
};
myObject myClass::obj(arguments to myObject constructor);
Couple of points: I agree it is not clear what the OP is trying to
achieve, but (1) why make obj public, since the OP only seems to require
"class wide access" (which I take as meaning access for class member/
static functions)? and (2) in the OP's code obj was a *pointer* to a
myObject ... not sure why, but maybe there is a valid reason (which he
hasn't told us).