Karl Heinz Buchegger wrote:[color=blue]
> "baumann@pan" wrote:[color=green]
> >
> > hi all,
> >
> > to implement a singleton class, one has define a static function in the
> > singleton class,
> > class A{
> > public:
> > static A* getInstance()
> > {
> > static A a;
> > return &a;
> > }
> > void print(); \\ print out 'a' I won't implement here.
> > void setA(int); \\ set 'a', I won't implement here.
> > ~A(){}
> > private:
> > A(){}
> > };
> >
> > int main()
> > {
> > A * a = A::getInstance();
> > a->setA(10);
> > a->print();
> > delete a; // <-- This line is a question. Read below.[/color]
>
> You don't need it.
> Actually it is an error.
> The object was not allocated with 'new', thus you have no
> reason to 'delete' it.
>[color=green]
> > return 0;
> > }
> >
> > i have the question about the above codes.
> > 1)
> > if the static function getInstance() doesn't have the access of the
> > private ctor function, how can static A a be defined without calling
> > the private ctor? so i conclude class static function can access the
> > ctor function.[/color]
>
> Because the static function is related to the class.
> In order to construct an object, the ctor must be accessible. Because
> the static function is a member of that class, it has access to the ctor
> and thus it can construct an object.[/color]
but it can not access other function of the class except ctor (or
dtor?).
t
[color=blue]
>[color=green]
> >
> > 2)
> > if the above is right,
> > i am confused why class static function can not access the data member
> > of that class but the ctor of the class?[/color]
>
> Please show code what you tried and what didn't work.[/color]
class A{
public:
static A* getInstance()
{
static A a;
i = 100;
print();
return &a;
}
void print(){
};
void setA(int){
};
private:
A(){}
int i;
};
int main()
{
A * a = A::getInstance();
a->setA(10);
a->print();
return 0;
}
the priavte data i can not be accessed by static function
getInstance();
and the member func print can not be called by the static function
either.
g++ emits :
singleton.cxx: In static member function `static A* A::getInstance()':
singleton.cxx:6: invalid use of member `A::i' in static member function
singleton.cxx:7: cannot call member function `void A::print()' without
object
[color=blue]
>[color=green]
> >
> > 3)
> > class granuity seems not applied for class static function?[/color]
>
> A static function can access anything of such an object, as it is
> a member of that class. The only thing is: it needs to have an object
> of that class type. Ordinary member functions can do this easily, since
> they are called for an object. Static functions are not called for a
> specific object, nevertheless they work in the context of that class and
> thus have potential access to anything in an object of that type as
> long as they somehow manage to get their fingers at an object.
>[/color]
you mean, if define a object of that class in the static function of
that class, the staitc functiion would have the access of whole
member(datum/functions) of the object of that class? and in this sense,
it conforms to the class granuity?
thanks much.[color=blue]
> --
> Karl Heinz Buchegger
>
kbuchegg@gascad.at[/color]