Hi,

We generally access non-static class members from a static method by passing
"this" to that static method and accessing non-static members over "this".

My alternative is caching "this" into a static member and use that static
member whenever you need to access non-static members. Is there anything bad
about this solution?

//////////////////////////////////////////////////// SAMPLE
///////////////////////////////////////

class Foo
{
public:
static Foo * lpThis;
Foo ()
{
// Some initializations...
lpThis = this; // Last statement in the constructor...
}

static void doJob ()
{
// Now we can access all non-static members over static "lpThis"...
}
};
Foo * Foo::lpThis;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////

Thanks in advance.

Re: Accessing non-static class members fom static methods (About by Alf

Alf
Wed Jul 23 08:09:19 CDT 2008

* Kürþat:
>
> We generally access non-static class members from a static method by passing
> "this" to that static method and accessing non-static members over "this".
>
> My alternative is caching "this" into a static member and use that static
> member whenever you need to access non-static members. Is there anything bad
> about this solution?

Yes.

First, if static member functions are called from non-static ones, with the
pointer updated to the "current" instance, it lets you violate constness without
knowing it.

I.e., you have then -- not that you're doing it below -- effectively
prevented the compiler from telling you about such constness violations.

Second, it assumes there is ever only one object of class Foo, or that you're
only interested in the last object instantiated from Foo.

Third, that's very dangerous with respect to that object's lifetime: it's
possible to make the static member functions use a dangling pointer, one that no
longer points to a valid object.

However, the second and third points can be circumvented by making Foo a
singleton class.

But a much better solution then is to make the current static member functions
into non-static member functions (look up "Meyers singleton").


> //////////////////////////////////////////////////// SAMPLE
> ///////////////////////////////////////
>
> class Foo
> {
> public:
> static Foo * lpThis;
> Foo ()
> {
> // Some initializations...
> lpThis = this; // Last statement in the constructor...
> }
>
> static void doJob ()
> {
> // Now we can access all non-static members over static "lpThis"...
> }
> };
> Foo * Foo::lpThis;
>
> /////////////////////////////////////////////////////////////////////////////////////////////////////////////
>
> Thanks in advance.

You're welcome.


- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Re: Accessing non-static class members fom static methods (About an alternative) by Igor

Igor
Wed Jul 23 12:02:06 CDT 2008

Alf P. Steinbach <alfps@start.no> wrote:
> * Kürþat:
>>
>> We generally access non-static class members from a static method by
>> passing "this" to that static method and accessing non-static
>> members over "this". My alternative is caching "this" into a static
>> member and use that
>> static member whenever you need to access non-static members. Is
>> there anything bad about this solution?
>
> First, if static member functions are called from non-static ones,
> with the pointer updated to the "current" instance, it lets you
> violate constness without knowing it.

How would that happen? This doesn't compile:

class C {
C* pThis;
public:
// Can't covert from const C* to C*
void f() const { pThis = this; }
};

I agree with other points, but I don't quite see how this particular
issue could occur.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925



Re: Accessing non-static class members fom static methods (About an alternative) by Igor

Igor
Wed Jul 23 12:07:04 CDT 2008

Krat <kursattheking@gmail.com> wrote:
> We generally access non-static class members from a static method by
> passing "this" to that static method and accessing non-static members
> over "this".

If this static method requires an instance pointer, why did you make it
static in the first place? What's the practical difference betwen

MyClass::method(pObj);

and

pObj->method();

apart from an uglier syntax in the former case?

> My alternative is caching "this" into a static member and use that
> static member whenever you need to access non-static members. Is
> there anything bad about this solution?

You seem to be reinventing a singleton (badly):

http://en.wikipedia.org/wiki/Singleton_pattern

--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925



Re: Accessing non-static class members fom static methods (About an alternative) by Ben

Ben
Wed Jul 23 13:17:31 CDT 2008

Kürþat wrote:
> Hi,
>
> We generally access non-static class members from a static method by
> passing "this" to that static method and accessing non-static members
> over "this".
> My alternative is caching "this" into a static member and use that
> static member whenever you need to access non-static members. Is
> there anything bad about this solution?

Lack of support for re-entrancy and concurrency come to mind, and that's
just the technical reasons not to.



Re: Accessing non-static class members fom static methods (About by Alf

Alf
Wed Jul 23 17:39:36 CDT 2008

* Igor Tandetnik:
> Alf P. Steinbach <alfps@start.no> wrote:
>> * Kürþat:
>>> We generally access non-static class members from a static method by
>>> passing "this" to that static method and accessing non-static
>>> members over "this". My alternative is caching "this" into a static
>>> member and use that
>>> static member whenever you need to access non-static members. Is
>>> there anything bad about this solution?
>> First, if static member functions are called from non-static ones,
>> with the pointer updated to the "current" instance, it lets you
>> violate constness without knowing it.
>
> How would that happen? This doesn't compile:
>
> class C {
> C* pThis;
> public:
> // Can't covert from const C* to C*
> void f() const { pThis = this; }
> };
>
> I agree with other points, but I don't quite see how this particular
> issue could occur.

I apologize for usign "unclear" :-) quoted terminology, it was meant to be
inferred from context.

Here's the OP's code:


<code from OP>
class Foo
{
public:
static Foo * lpThis;
Foo ()
{
// Some initializations...
lpThis = this; // Last statement in the constructor...
}

static void doJob ()
{
// Now we can access all non-static members over static "lpThis"...
}
};
Foo * Foo::lpThis;
</code from OP>


Now all we have to do is to introduce a const non-static member function, and
call doJOb from that:


<revised code>
<code from OP>
class Foo
{
public:
static Foo * lpThis;
Foo ()
{
// Some initializations...
lpThis = this; // Last statement in the constructor...
}

void blah() const { doJob(); }

static void doJob ()
{
// Now we can access all non-static members over static "lpThis"...
lpThis -> modify modify modify;
}
};
Foo * Foo::lpThis;

int main()
{
Foo const o;
o.blah();
}

</code from OP>


Cheers, & hth.,

- Alf


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Re: Accessing non-static class members fom static methods (About an alternative) by Ulrich

Ulrich
Thu Jul 24 02:12:24 CDT 2008

Kürþat wrote:
> We generally access non-static class members from a static method by
> passing "this" to that static method and accessing non-static members over
> "this".

Why make it static in the first place then? If you need an object as
context, make it a memberfunction.

> My alternative is caching "this" into a static member and use that static
> member whenever you need to access non-static members. Is there anything
> bad about this solution?

Yes. If something belongs to an object, make it part of that object.
Secondly, you solution will fall apart when used in a multithreaded
environment. Generally, all arguments why globals are bad apply to this
case, too, except that the scope is a bit smaller than global.

> class Foo
> {
> public:
> static Foo * lpThis;
> Foo ()
> {
> // Some initializations...
> lpThis = this; // Last statement in the constructor...
> }
>
> static void doJob ()
> {
> // Now we can access all non-static members over static
> "lpThis"...
> }
> };
> Foo * Foo::lpThis;

This looks like an attempt to create a so-called "singleton". Make a
websearch, there are much better ways to achieve the same. Not using a
singleton would be another alternative, too.

Uli

--
C++ FAQ: http://parashift.com/c++-faq-lite

Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

Re: Accessing non-static class members fom static methods (About an alternative) by Kürşat

Kürþat
Thu Jul 24 02:51:13 CDT 2008

Hi All,

I read all your replies and try to explain some points about my approac:

- My sample class (Foo) is only an axample and it should be instantiated as
a singleton. I will use a class like this as a Windows Threadpool wrapper
and while every process can have only one threadpool (before Vista) there is
no reason to create more than one instance.

- Why static? Because the only way to pass a class method to
QueueUserWorkItem () is making it static. Well, but if I make it static I
should meke every members it access static also. Very bad! QueueUserWorkItem
() expects only one parameter and the solution seems to pass "this" as its
only parameter. But I have a contex pointer which will be used by the
threadpool threads to execute my work. If I pass "this" as parameter how can
I pass the context pointer? One solution is creating a wrapper struct and
passing it to threadpool :

struct Param
{
LPVOID lpvThis;
LPVOID lpvParam;
};

Param * lpParam = new Param;
lpParam->lpvThis = this;
lpParam->lpvParam = lpContext;

Now I can pass "lpParam" and access both "this" and the "context". But the
promlem here is I should make one extra allocation and deallocation for
every queued work. I will use this class with an IOCP based server and
thousands of works will be queued and processed. So extra allocations and
deallocations will harm performance.

By caching "this" as a static pointer, I will eliminate this performance
penalty and pass only "context" object as I can access "this" via static
pointer.

I think reentrancy and other issues can be applied in both cases. There is
no defference between passing "this" as a parameter and caching it in a
static member as long as the object is singleton.

Am I right?

Thanks for your replies.

"Kürþat" <kursattheking@gmail.com> wrote in message
news:ePJnlCM7IHA.3512@TK2MSFTNGP03.phx.gbl...
> Hi,
>
> We generally access non-static class members from a static method by
> passing "this" to that static method and accessing non-static members over
> "this".
>
> My alternative is caching "this" into a static member and use that static
> member whenever you need to access non-static members. Is there anything
> bad about this solution?
>
> //////////////////////////////////////////////////// SAMPLE
> ///////////////////////////////////////
>
> class Foo
> {
> public:
> static Foo * lpThis;
> Foo ()
> {
> // Some initializations...
> lpThis = this; // Last statement in the constructor...
> }
>
> static void doJob ()
> {
> // Now we can access all non-static members over static "lpThis"...
> }
> };
> Foo * Foo::lpThis;
>
> /////////////////////////////////////////////////////////////////////////////////////////////////////////////
>
> Thanks in advance.
>
>
>
>



Re: Accessing non-static class members fom static methods (About by Alf

Alf
Thu Jul 24 04:00:53 CDT 2008

* Kürþat:
> I will use this class with an IOCP based server and
> thousands of works will be queued and processed. So extra allocations and
> deallocations will harm performance.
>
> By caching "this" as a static pointer, I will eliminate this performance
> penalty and pass only "context" object as I can access "this" via static
> pointer.
>
> I think reentrancy and other issues can be applied in both cases. There is
> no defference between passing "this" as a parameter and caching it in a
> static member as long as the object is singleton.
>
> Am I right?

You really shouldn't be thinking about a singleton as a runnable for a thread
/pool/.

The point of a pool is to have several runnables queued.

You should instead be thinking along the lines of

DWORD WINAPI ThreadProc( void* p )
{
static_cast<IRunnable*>(p)->run();
}


Cheers, & hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Re: Accessing non-static class members fom static methods (About an alternative) by Kürsat

Kürsat
Thu Jul 24 05:40:33 CDT 2008

But I should also pass a context pointer to run ().

"Alf P. Steinbach" <alfps@start.no> wrote in message
news:AMWdnTZ9w7JY2RXVnZ2dnUVZ_r3inZ2d@posted.comnet...
>* Kürþat:
>> I will use this class with an IOCP based server and thousands of works
>> will be queued and processed. So extra allocations and deallocations will
>> harm performance.
>>
>> By caching "this" as a static pointer, I will eliminate this performance
>> penalty and pass only "context" object as I can access "this" via static
>> pointer.
>>
>> I think reentrancy and other issues can be applied in both cases. There
>> is no defference between passing "this" as a parameter and caching it in
>> a static member as long as the object is singleton.
>>
>> Am I right?
>
> You really shouldn't be thinking about a singleton as a runnable for a
> thread /pool/.
>
> The point of a pool is to have several runnables queued.
>
> You should instead be thinking along the lines of
>
> DWORD WINAPI ThreadProc( void* p )
> {
> static_cast<IRunnable*>(p)->run();
> }
>
>
> Cheers, & hth.,
>
> - Alf
>
> --
> A: Because it messes up the order in which people normally read text.
> Q: Why is it such a bad thing?
> A: Top-posting.
> Q: What is the most annoying thing on usenet and in e-mail?
>



Re: Accessing non-static class members fom static methods (About by Alf

Alf
Thu Jul 24 06:07:13 CDT 2008

* Kürsat:
> But I should also pass a context pointer to run ().

Not necessary.

The object that run() is invoked on can hold any necessary context.

If virtual member functions are unfamiliar territory, you'll find a discussion
of them in your nearest C++ textbook; you can simply make a run() a virtual
function.

Cheers, & hth.,

- Alf


PS: Please respect the bottom-posting/top-posting style of the article you
respond to, and please don't quote signatures or other extraneous material.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?