473,842 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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


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@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
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@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
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
10963
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...
2
9604
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. #------------------------------------------------
2
2044
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...
3
3059
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
3350
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...
9
5001
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...
5
3167
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
3
3763
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...
0
2842
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...
0
9865
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9709
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10934
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10303
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7853
isladogs
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...
0
7025
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();...
0
5694
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...
2
4085
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3140
bsmnconsultancy
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...

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.