473,320 Members | 1,991 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

class granuity?

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
9 2941
"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


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


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


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

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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Murat Tasan | last post by:
hi, i am having a small problem with constructing an inner class. i am using an inner class (and not a static nested class) because the methods of the inner class need access to the enclosing...
2
by: Fernando Rodriguez | last post by:
Hi, I need to traverse the methods defined in a class and its superclasses. This is the code I'm using: # An instance of class B should be able to check all the methods defined in B #and A,...
2
by: Gabriel Genellina | last post by:
Hi In the following code sample, I have: - a Worker class, which could have a lot of methods and attributes. In particular, it has a 'bar' attribute. This class can be modified as needed. - a...
3
by: Robert | last post by:
Python doesn't know the class of a method when container not direct class attribute: >>> class X: .... def f():pass .... g=f .... l= .... >>> X.g <unbound method X.f>
1
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me...
9
by: Banaticus Bart | last post by:
I wrote an abstract base class from which I've derived a few other classes. I'd like to create a base class array where each element is an instance of a derived object. I can create a base class...
5
by: Andy | last post by:
Hi all, I have a site with the following architecture: Common.Web.dll - Contains a CommonPageBase class which inherits System.Web.UI.Page myadd.dll - Contains PageBase which inherits...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
0
by: emin.shopper | last post by:
I had a need recently to check if my subclasses properly implemented the desired interface and wished that I could use something like an abstract base class in python. After reading up on metaclass...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.