Re: Private constructor

From: Ron Natalie (ron_at_sensor.com)
Date: 12/21/03


Date: Sun, 21 Dec 2003 13:45:01 -0500


"Andy" <garth_rockett@yahoo.com> wrote in message news:96dbbc80.0312210934.4b902a49@posting.google.com...

> 1) Static member functions cannot access any non-static data members
> of a class.

They can if they access it through a pointer or a reference. You are right, they
can't without as they have no "this" object to get the member from. However, given
an object pointer they can get access to the private non-static members on the object.

>
> With constructors, this is different although they are non-static. The
> static member function can instantiate an object of the class it
> belongs to. This is what I found out.

It's not really that different. The static member can create an object of the
type because the constructor is ACCESSIBLE (i.e., the private/protected/public
rules).

You see there are two different principles here:

static versus non-static (I don't know what to call this distinction in general).
Access control (which is the public/private/static protected rules).
 



Relevant Pages

  • Re: Using a macro, can I change what type an object is being cast to?
    ... > class has a static member function that acts a dummy callback (called by the ... > pointer to a subclass) and uses that to call non-static member functions. ... > function before the subclass object has been constructed fully thus calling ...
    (comp.lang.cpp)
  • Re: P/Invoke MFC member
    ... Why not create a function which will make the call to the static member ... > exposes limited API functionality, ... > that returns a pointer to its CWinApp object. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: creating thread in C++
    ... This is defined behaviour as long as you always pass a non-NULL pointer ... to myclass together with the function pointer &myclass::threadstub to ... a static member function does not have ... required extern "C" linkage. ...
    (comp.programming.threads)
  • Re: Static/ non-static member access
    ... Static member functions certainly can use non-static members, ... have no "this" pointer and thus don't operate implicitly on an object ... // Placement new - constructs a T in the memory pointed to by temp. ...
    (microsoft.public.vc.language)
  • Re: Static data memer
    ... > I've 2 classes class A, ... But i want to make it a static member. ... > declaration is not a problem. ... > constructors for class B ...
    (comp.lang.cpp)

Loading