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 9 2837
"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
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
>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.
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
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.
"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
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
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.
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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,...
|
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...
|
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>
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
|
by: Johno34 |
last post by:
I have this click event on my form. It speaks to a Datasheet Subform
Private Sub Command260_Click()
Dim r As DAO.Recordset
Set r = Form_frmABCD.Form.RecordsetClone
r.MoveFirst
Do
If...
|
by: DizelArs |
last post by:
Hi all)
Faced with a problem, element.click() event doesn't work in Safari browser.
Tried various tricks like emulating touch event through a function:
let clickEvent = new Event('click', {...
| |