Home > Archive > VC Language > June 2005 > Static/ non-static member access
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Static/ non-static member access
|
|
|
| // in .h file
class CSingleton
{
public:
static CSingleton* Instance();
protected:
CSingleton();
CSingleton(const CSingleton&);
CSingleton& operator= (const CSingleton&);
~CSingleton();
private:
static CSingleton* pinstance;
};
// in .cpp file
CSingleton* CSingleton::pinstance = 0;
CSingleton::CSingleton()
{
}
CSingleton::~CSingleton()
{
}
CSingleton* CSingleton::Instance ()
{
if (pinstance == 0)
{
pinstance = new CSingleton(); //Line A
pinstance->DummyFn(); //Line B
//CSingleton sgTon;
}
return pinstance;
}
In Line A, in the above implementation of a Singleton class:
why is the static method Instance() allowed to create an instance of
the class on the heap, and thereby invoke the constructor (which is
non-static)?
Similarly, in Line B a non-static method 'DummyFn' is allowed to be invoked
from the static method. Why is it so?
TIA,
Sucharit
| |
| doug mansell 2005-05-31, 9:14 am |
| SD wrote:
> // in .h file
>
> class CSingleton
> {
> public:
> static CSingleton* Instance();
> protected:
> CSingleton();
> CSingleton(const CSingleton&);
> CSingleton& operator= (const CSingleton&);
> ~CSingleton();
>
> private:
> static CSingleton* pinstance;
> };
>
>
>
> // in .cpp file
>
> CSingleton* CSingleton::pinstance = 0;
>
> CSingleton::CSingleton()
> {
> }
>
> CSingleton::~CSingleton()
> {
> }
>
> CSingleton* CSingleton::Instance ()
> {
> if (pinstance == 0)
> {
> pinstance = new CSingleton(); //Line A
> pinstance->DummyFn(); //Line B
> //CSingleton sgTon;
> }
> return pinstance;
> }
>
>
> In Line A, in the above implementation of a Singleton class:
> why is the static method Instance() allowed to create an instance of
> the class on the heap, and thereby invoke the constructor (which is
> non-static)?
>
> Similarly, in Line B a non-static method 'DummyFn' is allowed to be invoked
> from the static method. Why is it so?
>
> TIA,
> Sucharit
>
You're about the restrictions of static methods.
The static methods of CSingleton have no 'this' pointer, but they are
perfectly able to call non-static methods of CSingleton instances.
| |
| Alex Blekhman 2005-05-31, 4:04 pm |
| doug mansell wrote:
> The static methods of CSingleton have no 'this' pointer,
> but they are perfectly able to call non-static methods of
> CSingleton instances.
Actually, constructor is not called by
CSingleton::Instance() but by operator new. If only class
members would be allowed to call constructor, then one
wouldn't be able to create any class instance at all.
| |
| Doug Harrison [MVP] 2005-05-31, 4:04 pm |
| On Tue, 31 May 2005 17:03:06 +0300, Alex Blekhman wrote:
> doug mansell wrote:
>
> Actually, constructor is not called by
> CSingleton::Instance() but by operator new. If only class
> members would be allowed to call constructor, then one
> wouldn't be able to create any class instance at all.
If we're going to be really pedantic, then "operator new" is an allocation
function that knows nothing of ctors, while the "new operator" is the
keyword "new" that does. The "new operator" has no impact on determining
whether or not an object can be created. To determine where you can create
objects, the thing to consider is accessibility, and the question to ask
is, "Does the calling code have access to the ctor and if applicable, the
class-specific operator new?" Static member functions of course have access
to the whole class, so they can create objects of their class. Now, if we
ignore accessibility, a static member function is no different than a
non-member function, and your second sentence follows naturally from that
perspective.
--
Doug Harrison
Microsoft MVP - Visual C++
| |
| Alex Blekhman 2005-05-31, 9:00 pm |
| Doug Harrison [MVP] wrote:
> If we're going to be really pedantic, then "operator new"
> is an allocation function that knows nothing of ctors,
> while the "new operator" is the keyword "new" that does.
Yes, you're right here. I intended to more generic meaning
of "new" that includes both allocation and object creation.
| |
|
| Hi Doug,
Thanks for your reply. I'm still a bit about this though.
a) Since static member functions cannot invoke any non-static methods,
how does it allow the "new operator" (which calls the constructor
(non-static))
to be invoked?
b) Since you've mentioned "operator new" and "new operator", could you
please elaborate on their differences?
TIA,
SD
"Doug Harrison [MVP]" wrote:
> On Tue, 31 May 2005 17:03:06 +0300, Alex Blekhman wrote:
>
>
> If we're going to be really pedantic, then "operator new" is an allocation
> function that knows nothing of ctors, while the "new operator" is the
> keyword "new" that does. The "new operator" has no impact on determining
> whether or not an object can be created. To determine where you can create
> objects, the thing to consider is accessibility, and the question to ask
> is, "Does the calling code have access to the ctor and if applicable, the
> class-specific operator new?" Static member functions of course have access
> to the whole class, so they can create objects of their class. Now, if we
> ignore accessibility, a static member function is no different than a
> non-member function, and your second sentence follows naturally from that
> perspective.
>
> --
> Doug Harrison
> Microsoft MVP - Visual C++
>
| |
| Arnaud Debaene 2005-06-01, 4:00 am |
| SD wrote:
> Hi Doug,
>
> Thanks for your reply. I'm still a bit about this though.
>
> a) Since static member functions cannot invoke any non-static methods,
> how does it allow the "new operator" (which calls the constructor
> (non-static))
> to be invoked?
Think about it like the "main" function (which can be considered static, as
all global functions functions) can create objects by "newing" them.
Arnaud
MVP - VC
| |
| Doug Harrison [MVP] 2005-06-01, 4:03 pm |
| On Tue, 31 May 2005 22:04:01 -0700, SD wrote:
> Hi Doug,
>
> Thanks for your reply. I'm still a bit about this though.
>
> a) Since static member functions cannot invoke any non-static methods,
> how does it allow the "new operator" (which calls the constructor
> (non-static))
> to be invoked?
Static member functions certainly can use non-static members, but as they
have no "this" pointer and thus don't operate implicitly on an object
pointed to by "this", they require an object to access its members.
Consider this:
struct X
{
void f1();
void g1()
{
X x; // Means create an X
f1(); // Means this->f1()
}
static void g2()
{
X x; // Means create an X - fine
f1(); // Oops, no this pointer in static member!
x.f1(); // Calls f1 through x - fine
}
};
The only thing creating objects with new adds to this is that in addition
to the ctor, the relevant operator new must be accessible, like I said last
time.
> b) Since you've mentioned "operator new" and "new operator", could you
> please elaborate on their differences?
Scott Meyers discussses this in "More Effective C++", but briefly, the "new
operator" is the "new" in new-expressions such as:
T* p = new T;
It amounts to this (simplified pseudocode):
T* temp = operator new(sizeof(T));
try
{
// Placement new - constructs a T in the memory pointed to by temp.
new (temp) T;
}
catch (...)
{
operator delete(temp);
throw;
}
return temp;
So you see, the compiler writes a bunch of stuff behind the scenes to
implement the "new operator", and it calls an allocation function named
"operator new". You can write your own "operator new" (and deallocation
function operator delete), but you can't change the "new operator", which
is not a function.
--
Doug Harrison
Microsoft MVP - Visual C++
|
|
|
|
|