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 2955
"baumann@pa n" 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@pa n" 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@pa n" <ba*********@gm ail.com> skrev i en meddelelse
news:11******** **************@ g44g2000cwa.goo glegroups.com.. .
Karl Heinz Buchegger wrote: "baumann@pa n" 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@pa n" <ba*********@gm ail.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.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 "granularit y" 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 object's members.
now, i have an enclosing class, which has a static factory method (loads
an instance of the enclosing class from a serialized file, and returns
it).
during the loading, some initialization takes place. one of the
initialization...
|
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, while an instance of class C should be able to check all methods
#defined in C, 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 Base class (old-style) which defines a default class attribute 'bar'
too. I can't modify the source code of this class. Note that Workes does
not inherit from Base.
- a Derived class which must inherit from Base and is a wrapper around
Worker: it...
|
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 to turn a previously
made String class that deals with char's into a templated String class
that uses the template parameter C instead of char. I thought it would
be fairly simple to do this exercise, but I encoutered many errors for
my...
| |
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 pointer
which points to an instance of a derived class, but when I pass that
base class pointer into a function, it can't access the derived
object's public functions. Although, the base class pointer does call
the appropriate virtual function...
|
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 CommonPageBase
- Contains myPage which inherits PageBase
Each of these classes overrides OnInit and ties an event handler
|
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 thawed it out. I built a console app using
Microsoft Visual C++ 6 (VC++) and it worked great. Only one line in the
header file had to be commented out.
I built a console app using Borland C++ Builder 5. The linker complained of
references to...
|
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 magic, I
wrote the following module. It is mainly useful as a light weight tool
to help programmers catch mistakes at definition time (e.g., forgetting
to implement a method required by the given interface). This is handy
when unit tests or...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |