Re: What's wrong withthe code?

From: Kevin Goodsell (usenet2.spamfree.fusion_at_neverbox.com)
Date: 04/19/04


Date: Sun, 18 Apr 2004 22:26:42 GMT

Rob Williscroft wrote:

> Kevin Goodsell wrote in news:S2Agc.19892$A_4.3903
> @newsread1.news.pas.earthlink.net in comp.lang.c++:
>
>>
>>Can you really have a static member that is the same type as the class
>>it is a member of? What would that even mean? Would you have infinitely
>>nested 'membs' that are really all the same object?
>>
>
>
> static members are members of the class (of which there is only one).
>
> So the static instance's class has a member which is the instance
> itself, recursion would be if the instance *contained* itself, which
> it doesn't.
>
> Similar perhaps to an object that contains a /pointer/ to itself.
>
> Rob.

So the following is legal, if I understand correctly:

#include <iostream>

struct Test
{
   static Test t;
};

Test Test::t;

int main()
{
   Test a;

   if (&(a.t.t.t.t.t.t.t.t.t.t.t.t.t.t.t.t.t) == &(a.t))
   {
     std::cout << "Hmmmm..." << std::endl;
   }

   return 0;
}

g++ (-W -Wall -ansi -pedantic) accepts this and prints "Hmmmm...". It
shows what I meant by "infinitely nested [objects] that are really all
the same object". It makes sense (sort of), but seems strange to me.

-Kevin

-- 
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.


Relevant Pages

  • Re: EnumChildWindows and C++
    ... I have a good stack of reading material now. ... Generally a *public* static member function can be used ... > whereever a C-style non-member function can be used. ...
    (microsoft.public.win32.programmer.ui)
  • Re: Creating thread from a class object
    ... and non-static member functions and data of any instance of the ... forth having that type and use all the private members. ... << o) using only the public interface, the fact is that static member ...
    (microsoft.public.vc.language)
  • Re: Microsoft Data Access Block and static methods
    ... "If User A calls my web service at the same time User B calls it, ... "I would figure it to be completely opposite - that if multiple threads ... returned to the wrong user, whereas in an instance member, each method ... not, I am), before writing or accessing a static member. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Methods and Threading
    ... My backgroundCalc function needs to access several private members such ... as GPIBDevice1, 2, and the a static text member in the form (all of ... Therefore, using a static member ...
    (microsoft.public.vc.mfc)