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

L

Lucy Ludmiller

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() ?
 
T

Thomas Tutone

Lucy said:
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
 
R

Radu

Thomas said:
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
 
T

Thomas Tutone

Radu said:
... 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top