By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,723 Members | 832 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,723 IT Pros & Developers. It's quick & easy.

class granuity?

P: n/a
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.
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.

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?

3)
class granuity seems not applied for class static function?
baumann@pan

Jul 23 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
"baumann@pan" wrote:

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.
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.
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.
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.

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?
Please show code what you tried and what didn't work.

3)
class granuity seems not applied for class static function?


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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #2

P: n/a


baumann@pan wrote:
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();
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.

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?

3)
class granuity seems not applied for class static function?
baumann@pan

Jul 23 '05 #3

P: n/a
>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.
Any method in class can access EVERY method in the same class.
keywords 'private', 'protected' and 'public' affects another class who
use/derived it.
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?


Because static method can be called without creating actual object. If
static function can access data member, what happen if those data
member are not exists.

Regards,
Prawit C.

Jul 23 '05 #4

P: n/a


Karl Heinz Buchegger wrote:
"baumann@pan" wrote:

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.
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.
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.


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.


but it can not access other function of the class except ctor (or
dtor?).

t

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?
Please show code what you tried and what didn't work.


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

3)
class granuity seems not applied for class static function?
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.


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. --
Karl Heinz Buchegger
kb******@gascad.at


Jul 23 '05 #5

P: n/a


Prawit Chaivong wrote:
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.
Any method in class can access EVERY method in the same class.
keywords 'private', 'protected' and 'public' affects another class who
use/derived it.
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?


Because static method can be called without creating actual object. If
static function can access data member, what happen if those data
member are not exists.


good explaination!

according to it, i infer
1)dtor can not be called, but dtor are bound to the class object, so if
the object exists, the object can call dtor.

2) when define a object of that class in the static function, ctor of
the class is to be called first, there is no contradict with your
description.
static function of a class can access all of the class.

thanks
Regards,
Prawit C.


Jul 23 '05 #6

P: n/a

"baumann@pan" <ba*********@gmail.com> skrev i en meddelelse
news:11**********************@g44g2000cwa.googlegr oups.com...


Karl Heinz Buchegger wrote:
"baumann@pan" wrote:
>
[snip]
>
> 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?
Please show code what you tried and what didn't work.


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.


This is because getInstance is static. The meaning of static is that there
is no object (no "this-pointer") for the function.

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
In other words: you can't use a member variable in a static functgion. singleton.cxx:7: cannot call member function `void A::print()' without
object

It is more clear here - you have no object.

Change your function as follows:

static A* getInstance()
{
static A a;
a.i = 100;
a.print();
return &a;
}

[snip]

/Peter
Jul 23 '05 #7

P: n/a
baumann@pan wrote:

Karl Heinz Buchegger wrote:
Please show code what you tried and what didn't work.

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;
}


Change to :

static A* getInstance()
{
static A a;
a.i = 100;
a.print();
return &a;
}
Normal member functions are associated with a particular object when
they are called (i.e., the object pointed to by the 'this' pointer).
Static member functions are associated with a class (and thus have the
same access rights to all the members), but are not associated with a
particular object.

-Alan
Jul 23 '05 #8

P: n/a
baumann@pan wrote:
> 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.
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.


but it can not access other function of the class except ctor (or
dtor?).


Again, it is a member of the class, and so it can access any private members
of the same class.
> 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?


Please show code what you tried and what didn't work.


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


Well, getInstance() is static, so it doesn't have an object associated with
it. Which object would print() be called for? Try instead:

static A* getInstance()
{
static A a;
a.i = 100;
a.print();
return &a;
}
> 3)
> class granuity seems not applied for class static function?


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.


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?


Yes.
and in this sense, it conforms to the class granuity?


Yes.

Jul 23 '05 #9

P: n/a

"baumann@pan" <ba*********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...

3)
class granuity seems not applied for class static function?


What's "granuity"? I can't find it in my dictionary, nor on the Yahoo or
Mirriam-Webster online dictionaries.

Did you mean "granularity" perhaps?

-Howard
Jul 23 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.