Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > static function calling nonstatic method ? (or something like that)

Reply
Thread Tools

static function calling nonstatic method ? (or something like that)

 
 
Lucy Ludmiller
Guest
Posts: n/a
 
      09-01-2006
I have code like this :

void foo(int i, void* p) { .. }


class A
{
public:
A();
~A();

private:
void foobar(int i, char* c);
};



Is there a way for foo to call foobar() ?

 
Reply With Quote
 
 
 
 
Thomas Tutone
Guest
Posts: n/a
 
      09-01-2006
Lucy Ludmiller wrote:
> I have code like this :
>
> void foo(int i, void* p) { .. }
>
> class A
> {
> public:
> A();
> ~A();
> private:
> void foobar(int i, char* c);
> };
>
> Is there a way for foo to call foobar() ?


Sure:

void foo(int i, void* p)
{
A a;
a.foobar(i, static_cast<char*>(p));
}

But if your question is, can foo call foobar without an instance of
class A to call it with, the answer is no, unless you change foobar to
be a static member of A.

Best regards,

Tom

 
Reply With Quote
 
 
 
 
Radu
Guest
Posts: n/a
 
      09-01-2006

Thomas Tutone wrote:
> Lucy Ludmiller wrote:
> > I have code like this :
> >
> > void foo(int i, void* p) { .. }
> >
> > class A
> > {
> > public:
> > A();
> > ~A();
> > private:
> > void foobar(int i, char* c);
> > };
> >
> > Is there a way for foo to call foobar() ?

>
> Sure:
>
> void foo(int i, void* p)
> {
> A a;
> a.foobar(i, static_cast<char*>(p));
> }


.... if class A declares foo as a friend:
class A
{
friend void foo(int i, void* p) ;
//.....
};

HTH
Radu

>
> But if your question is, can foo call foobar without an instance of
> class A to call it with, the answer is no, unless you change foobar to
> be a static member of A.
>


you can declaring
> Best regards,
>
> Tom


 
Reply With Quote
 
Thomas Tutone
Guest
Posts: n/a
 
      09-01-2006
Radu wrote:
> Thomas Tutone wrote:
> > Lucy Ludmiller wrote:
> > > I have code like this :
> > >
> > > void foo(int i, void* p) { .. }
> > >
> > > class A
> > > {
> > > public:
> > > A();
> > > ~A();
> > > private:
> > > void foobar(int i, char* c);
> > > };
> > >
> > > Is there a way for foo to call foobar() ?

> >
> > Sure:
> >
> > void foo(int i, void* p)
> > {
> > A a;
> > a.foobar(i, static_cast<char*>(p));
> > }

>
> ... if class A declares foo as a friend:
> class A
> {
> friend void foo(int i, void* p) ;
> //.....
> };


Good point - I missed that foo was private. Thanks for the
clarification.

Best regards,

Tom

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
nonstatic method glitch? countnoobula Software 1 10-19-2010 07:11 AM
static/nonstatic data member declaration/definition Jeffrey C++ 7 09-30-2008 09:01 AM
Returning a pointer to a nonstatic member function? Clay_Culver@yahoo.com C++ 3 11-04-2005 12:47 PM
Static/NONstatic error!! Jinesh C++ 8 01-06-2004 07:54 AM
Re: Can't make a static reference to nonstatic variable con in class X. Pedro López Java 0 10-13-2003 11:15 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57