Static Method accessing private methods

Static Method accessing private methods

Post by Anku » Sat, 07 Dec 2002 00:00:46



I needed to write a static method of a class which needs to return
some data for which it needs to use the private methods of the same
class. I tried one thing which worked but I am not sure why it worked.

class temp
{
public:
static void seti();
private:
void geti();
...

Quote:}

temp::seti()
{
temp t;
t.geti();

Quote:}

Here in seti() I am making a object of class temp. Here I should not
be able to use the geti() since its a private member variable. But
when I tried this I was able to use this.

Can anyone please tell how is it that for a object I can use the
private methods.

Thanks,
Ankur

 
 
 

Static Method accessing private methods

Post by Victor Bazaro » Sat, 07 Dec 2002 00:30:22



> I needed to write a static method of a class which needs to return

If it needs to return something, it can't be 'void', can it?

Quote:> some data for which it needs to use the private methods of the same
> class. I tried one thing which worked but I am not sure why it worked.

It worked????  Really?  A void function managed to return
something?

Quote:

> class temp
> {
> public:
> static void seti();
> private:
> void geti();

'get' has return type "void"?  Are you sure you know what
you're doing?

Quote:> ...
> }

Semicolon missing here.

Quote:

> temp::seti()

void temp::seti() // to be syntactically correct

Quote:> {
> temp t;
> t.geti();
> }

> Here in seti() I am making a object of class temp. Here I should not
> be able to use the geti() since its a private member variable.

Why?  'seti' is a member function, isn't it?  "private" means
accessible to friends and members, doesn't it?  Why do you say
that you "should not be able to use" geti?

Quote:> But
> when I tried this I was able to use this.

> Can anyone please tell how is it that for a object I can use the
> private methods.

I don't understand your confusion.  Read again what "private"
access specifier means.

Victor
--
Please remove capital A's from my address when replying by mail

 
 
 

Static Method accessing private methods

Post by Jakob Bielin » Sat, 07 Dec 2002 00:37:51



Quote:> I needed to write a static method of a class which needs to return
> some data for which it needs to use the private methods of the same
> class. I tried one thing which worked but I am not sure why it worked.

> class temp
> {
> public:
> static void seti();
> private:
> void geti();
> ...
> }

> temp::seti()
> {
> temp t;
> t.geti();
> }

> Here in seti() I am making a object of class temp. Here I should not
> be able to use the geti() since its a private member variable.

    Nope, 'geti ()' is a private member function. It's no variable.

Quote:> But when I tried this I was able to use this.

    'static' has nothing to do with access rights at all. 'static' only
means that this member can be called without having created an instance of
that class.

Quote:> Can anyone please tell how is it that for a object I can use the
> private methods.

    I don't really understand your question here. But private methods or
variables can only be accessed by members of the same class or friend
classes/function. Deriving or totally different classes as well as other
funcitons may not access the private class members.

    You should read up on how classes work and what the different specifiers
do. A place to start would be http://www.cplusplus.com

hth
--
jb

(replace y with x if you want to reply by e-mail)

 
 
 

Static Method accessing private methods

Post by Rolf Magnu » Sat, 07 Dec 2002 01:04:13



> Here in seti() I am making a object of class temp. Here I should not
> be able to use the geti() since its a private member variable. But
> when I tried this I was able to use this.

private works on a per-class basis, not per-object. Since seti() is a
member of the class, it can access any private member of the same
class.
 
 
 

Static Method accessing private methods

Post by Panzers Eas » Sat, 07 Dec 2002 03:04:04


Quote:>     'static' has nothing to do with access rights at all. 'static' only
> means that this member can be called without having created an instance of
> that class.

> jb

What about 'static' member functions?  Correct me if I'm wrong but static
member functions cannot access nonstatic data members of objects of the
class.  In this case, static does indicate access rights, so to speak.  Of
course, he's talking about static variables so the point is moot--I just
didn't want him to get the wrong idea about 'static'.

Troy S.

 
 
 

Static Method accessing private methods

Post by Neil Butterwort » Sat, 07 Dec 2002 03:08:04



Quote:> >     'static' has nothing to do with access rights at all. 'static' only
> > means that this member can be called without having created an instance
of
> > that class.

> > jb

> What about 'static' member functions?  Correct me if I'm wrong but static
> member functions cannot access nonstatic data members of objects of the
> class.

Yes they can:

class A {
 private:
  int x;
 public:
  static void Foo( A & a ) {
   a.x = 42;
  }

Quote:};

int main() {
 A a;
 A::Foo( a );

Quote:}

NeilB
 
 
 

Static Method accessing private methods

Post by Jakob Bielin » Sat, 07 Dec 2002 03:12:32



Quote:> >     'static' has nothing to do with access rights at all. 'static' only
> > means that this member can be called without having created an instance
of
> > that class.

> > jb

> What about 'static' member functions?  Correct me if I'm wrong but static
> member functions cannot access nonstatic data members of objects of the
> class.  In this case, static does indicate access rights, so to speak.  Of
> course, he's talking about static variables so the point is moot--I just
> didn't want him to get the wrong idea about 'static'.

    You may access all types: private, protected and public variables. The
fact that you are restricted to static members is what 'static' is for.
Basically, you are right in saying that 'static' does indeed restrict
access. But it doesn't restrict it in the way the OP described it (or at
least in the way I understood how the OP described it).

regards
--
jb

(replace y with x if you want to reply by e-mail)

 
 
 

Static Method accessing private methods

Post by Panzers Eas » Sat, 07 Dec 2002 04:14:10


Quote:> > What about 'static' member functions?  Correct me if I'm wrong but
static
> > member functions cannot access nonstatic data members of objects of the
> > class.

> Yes they can:

Okay, I'm obviously confused by what I've read.  Please explain the
following passage from Accelerated C++, page 244-245: "Static member
functions differ from ordinary functions in that they do not operate on an
object of the class type.  As such, they cannot access the nonstatic data
members of objects of the class: There is no object associated with the
function, so there are no members to use."

Troy S.

 
 
 

Static Method accessing private methods

Post by Jakob Bielin » Sat, 07 Dec 2002 04:19:40



Quote:> > > What about 'static' member functions?  Correct me if I'm wrong but
> static
> > > member functions cannot access nonstatic data members of objects of
the
> > > class.

> > Yes they can:

> Okay, I'm obviously confused by what I've read.  Please explain the
> following passage from Accelerated C++, page 244-245: "Static member
> functions differ from ordinary functions in that they do not operate on an
> object of the class type.  As such, they cannot access the nonstatic data
> members of objects of the class: There is no object associated with the
> function, so there are no members to use."

    By default, static member functions don't have an object associated (no
'this' pointer), thus you can't access non-static data, because you would
not know which object's data you are accessing. But if you use Neil's
example (passing an instance of the class to the static member function),
they are allowed to access all members of that class instance.

regards
--
jb

(replace y with x if you want to reply by e-mail)

 
 
 

Static Method accessing private methods

Post by Neil Butterwort » Sat, 07 Dec 2002 04:23:42



Quote:> > > What about 'static' member functions?  Correct me if I'm wrong but
> static
> > > member functions cannot access nonstatic data members of objects of
the
> > > class.

> > Yes they can:

> Okay, I'm obviously confused by what I've read.  Please explain the
> following passage from Accelerated C++, page 244-245: "Static member
> functions differ from ordinary functions in that they do not operate on an
> object of the class type.  As such, they cannot access the nonstatic data
> members of objects of the class: There is no object associated with the
> function, so there are no members to use."

As the code I posted shows, static members can access private members of the
class. I would say the following  sentence is incorrect or at least badly
phrased:

Quote:> As such, they cannot access the nonstatic data
> members of objects of the class:

Perhaps Andrew Koenig would like to comment?

NeilB

 
 
 

Static Method accessing private methods

Post by Victor Bazaro » Sat, 07 Dec 2002 04:26:07



> > > What about 'static' member functions?  Correct me if I'm wrong but
> static
> > > member functions cannot access nonstatic data members of objects of
the
> > > class.

> > Yes they can:

> Okay, I'm obviously confused by what I've read.  Please explain the
> following passage from Accelerated C++, page 244-245: "Static member
> functions differ from ordinary functions in that they do not operate on an
> object of the class type.  As such, they cannot access the nonstatic data
> members of objects of the class: There is no object associated with the
> function, so there are no members to use."

class A {
    void NonstaticMemberFunc();
    static void StaticMemberFunc();
    void SomeOtherNonstaticMemberFunc() {}

Quote:};

void A::NonstaticMemberFunc()
{
    SomeOtherNonstaticMemberFunc();     // allowed because it's
                                          // the same as
    this->SomeOtherNonstaticMemberFunc();   // this (no pun intended)

Quote:}

void A::StaticMemberFunc()
{
    SomeOtherNonstaticMemberFunc();     // NOT allowed because it's
                                          // the same as
    this->SomeOtherNonstaticMemberFunc();   // this (no pun intended)

    // and static functions do not have "this" (they are not called
    //                                  with a particular object)

Quote:}

int main()
{
    A a;
    a.NonstaticMemberFunc(); // address of 'a' is 'this' in it
    a.StaticMemberFunc();   // address of 'a' is not passed
    A::StaticMemberFunc(); // you can call it like that too

Quote:}

Victor
--
Please remove capital A's from my address when replying by mail
 
 
 

Static Method accessing private methods

Post by Alexander Terekho » Sat, 07 Dec 2002 04:33:41


[...]

Quote:> It worked????  Really?  A void function managed to return
> something?

Sure. Bazarov, try this:

void f() {
  return f();

Quote:}

regards,
alexander.
 
 
 

Static Method accessing private methods

Post by Rolf Magnu » Sat, 07 Dec 2002 05:43:35




> [...]
>> It worked????  Really?  A void function managed to return
>> something?

> Sure. Bazarov, try this:

> void f() {
>   return f();
> }

I guess that this would segfault quite fast on most machines. Btw, your
example never returns :)
 
 
 

Static Method accessing private methods

Post by Joe Clark » Sat, 07 Dec 2002 06:18:21



Quote:> > > What about 'static' member functions?  Correct me if I'm wrong but
> static
> > > member functions cannot access nonstatic data members of objects of
the
> > > class.

> > Yes they can:

> Okay, I'm obviously confused by what I've read.  Please explain the
> following passage from Accelerated C++, page 244-245: "Static member
> functions differ from ordinary functions in that they do not operate on an
> object of the class type.  As such, they cannot access the nonstatic data
> members of objects of the class: There is no object associated with the
> function, so there are no members to use."

You are confused because Niel failed to say that an instance to an object
must be passed to the static function. It is statements like Niel's that
just say "Yes you can" without explaing the conditions -- i.e. just posting
a code example that works -- that cause confusion and frustration.

Additionally, any function can access the private members of a class.

(Oh Niel, I better clarify for you that the function must be a friend
function to the class)

 
 
 

Static Method accessing private methods

Post by Neil Butterwort » Sat, 07 Dec 2002 06:33:52





> > > > What about 'static' member functions?  Correct me if I'm wrong but
> > static
> > > > member functions cannot access nonstatic data members of objects of
> the
> > > > class.

> > > Yes they can:

> > Okay, I'm obviously confused by what I've read.  Please explain the
> > following passage from Accelerated C++, page 244-245: "Static member
> > functions differ from ordinary functions in that they do not operate on
an
> > object of the class type.  As such, they cannot access the nonstatic
data
> > members of objects of the class: There is no object associated with the
> > function, so there are no members to use."

> You are confused because Niel failed to say that an instance to an object
> must be passed to the static function. It is statements like Niel's that
> just say "Yes you can" without explaing the conditions -- i.e. just
posting
> a code example that works -- that cause confusion and frustration.

The code example that I posted illustrates exactly this.

Quote:> Additionally, any function can access the private members of a class.

> (Oh Niel, I better clarify for you that the function must be a friend
> function to the class)

Perhaps you ought to clarify that a friend function must also have an
instance of the class passed to it?

Moron.

NeilB

 
 
 

1. Accessing inherited public static methods of a private base class

//      I wonder why the inherited public STATIC member B::f() is
//      accessible in global_function() but not in D::foo2().
//
//      According to ARM section 11.2, the call to B::g() in D::foo4()
//      is allowed to avoid the absurdity of a member function
//      having less access than a global function.  But the
//      statement on p.243 states only that: "Specifying a base
//      class private does not affect access to static members of
//      the base class."  It does not seem to be strong enough.  I
//      think it should say something like "Specifying a base class
//      private does not affect access to static members of the base
//      class itself and those of its bases."
//      
//      Does ANSI say anything about this?
//
//      Ted Law

#include <stdio.h>

class A {
  public:
    static void f() { printf( "A::f()\n" ); }

class B : public A {
  public:
    static void g() { printf( "B::g()\n" ); }

class C : private B {
  public:
    void foo() { B::f(); }      // OK

class D : public C {
  public:
    void foo1() { A::f(); }     // OK
    void foo2() { B::f(); }     // A::f() inaccessible
    void foo4() { B::g(); }     // OK

void global_function()
{
    A::f();                     // OK
    B::f();                     // OK
                                // This shows the absurdity that ARM
                                // tries to avoid

2. HELP!!!

3. can I give a pointer to a private static method to an extern C-function?

4. PC-select

5. How do I access another type's method from one type's method?

6. Apple2 Hardware, Software, Repairs

7. How do you access private data in a different class method?

8. Hot-swappable hard disks

9. Method '~' of object '~' failed on getFirst method of cdo message object

10. termination of thread that called native method before native method returns response

11. Methods returning pointers to methods

12. Const Method using non-const methods

13. Using base method code when overriding methods