How to access a class member function outside of its class?

J

jt

Being a newbie in C++ and comming from C, I can't find information on how to
access a class member function outside its class.

Below is a snippet of the class and its member function: look at
"AddCallPages"
=============================================================
class CPsnstatuspttView : public CFormView
{
protected: // create from serialization only
CPsnstatuspttView();
DECLARE_DYNCREATE(CPsnstatuspttView)

public:
//{{AFX_DATA(CPsnstatuspttView)
enum { IDD = IDD_PSNSTATUSPTT_FORM };
CEdit m_edit_msgs_ctrl;
CString m_edit_messages;
//}}AFX_DATA

// Attributes
public:
CPsnstatuspttDoc* GetDocument();

// Operations
public:

// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CPsnstatuspttView)
public:
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
virtual void OnInitialUpdate(); // called first time after construct
//}}AFX_VIRTUAL

// Implementation
public:

CString m_ConnectPort;
CString m_ConnectHost;

void AddCallPages(void); .. <------------------------- this function I
would like to call from another module
=========================================================================

Or is there a different easier way to do this outside the class maybe?

Thank you for your help.
JT
 
V

Victor Bazarov

jt said:
Being a newbie in C++ and comming from C, I can't find information on how
to
access a class member function outside its class.
[...]

I don't know what book you're reading that doesn't talk about member
access, but in short you need an object (an instance) of that class
with which you call your member function. The object can be identified
by a variable, by a reference, or by a pointer. For the latter you use
the -> operator to get to a member, for the former two you use the 'dot'
(just like in C). How do you find a member of a struct in C? Have you
ever used a struct in C? Well, that's basically the same thing. Only
if your member is a function, you need to follow its name with the list
of arguments in parentheses, just like with any other function.

class A {
int i;
public:
A(int i_) : i(i_) {}
int increment(int a) { return i += a; }
int decrement(int a) { return i -= a; }
};

int main() {
A a(42);
a.increment(77);
A& ra = a;
ra.decrement(33);
A *pa = &a;
pa->increment(100);
}

V
 
J

jt

I'm sorry, that isn't what I was asking. You misread the question or I
didn't state it correctly.

I already know this what you said below.

I will try again with my question. I wish to call a function that is in
another class from outside its module.
Using your example say I wish to call A.increment, but do it inside myB.cpp
:
class A {
int i;
public:
A(int i_) : i(i_) {}
int increment(int a) { return i += a; }
int decrement(int a) { return i -= a; }
};

Now I have another cpp file called myB.cpp:
class B {
int i;
public:
B(int i_) : i(i_) {}
int add(int a) { return i += a; }
int subtract(int a) { return i -= a; }
};

Being In file myB.cpp, how can I can call class A.increment?

JT

Victor Bazarov said:
jt said:
Being a newbie in C++ and comming from C, I can't find information on how
to
access a class member function outside its class.
[...]

I don't know what book you're reading that doesn't talk about member
access, but in short you need an object (an instance) of that class
with which you call your member function. The object can be identified
by a variable, by a reference, or by a pointer. For the latter you use
the -> operator to get to a member, for the former two you use the 'dot'
(just like in C). How do you find a member of a struct in C? Have you
ever used a struct in C? Well, that's basically the same thing. Only
if your member is a function, you need to follow its name with the list
of arguments in parentheses, just like with any other function.

class A {
int i;
public:
A(int i_) : i(i_) {}
int increment(int a) { return i += a; }
int decrement(int a) { return i -= a; }
};

int main() {
A a(42);
a.increment(77);
A& ra = a;
ra.decrement(33);
A *pa = &a;
pa->increment(100);
}

V
 
M

Malte Starostik

jt said:
I'm sorry, that isn't what I was asking. You misread the question or I
didn't state it correctly.

I already know this what you said below.

I will try again with my question. I wish to call a function that is in
another class from outside its module.
Using your example say I wish to call A.increment, but do it inside myB.cpp

Pretty much the same wa you would in C. Declare class A in myA.h (like
prototypes in C) and define it in myA.cpp. Then #include myA.h in both
myA.cpp (so the declaration is known to the compiler when it sees the
definition) and myB.cpp (so the compiler knows about class A):

myA.h:

#ifndef MYA_H
#define MYA_H

class A
{
int i;
public:
A( int );
int increment( int );
int decrement( int );
};

#endif

myA.cpp:

A::A( int i_ )
: i( i_ )
{
}

int A::increment( int a )
{
return i += 1;
}

int A::decrement( int a )
{
return i -= 1;
}

myB.cpp:

int main()
{
A a( 42 );
a.increment( 666 );
}

Of course such trivial functions are candidates for inline definitions
inside the header, just so you get the idea.
myA.h now is somewhat similar to a C header like this:

typedef struct
{
int i;
} A;

extern void init_A( struct A*, int ); /* A::A( int ) */
extern int increment_A( struct A*, int ); /* A::increment( int ) */
extern int decrement_A( struct A*, int ); /* A::decrement( int ) */

Cheers,
Malte
 
M

modemer

jt said:
I'm sorry, that isn't what I was asking. You misread the question or I
didn't state it correctly.

I already know this what you said below.

I will try again with my question. I wish to call a function that is in
another class from outside its module.
Using your example say I wish to call A.increment, but do it inside myB.cpp

Now I have another cpp file called myB.cpp:


Being In file myB.cpp, how can I can call class A.increment?

Just like C, in myB.cpp, you should include declaration of class A, and have
an object or reference of class A in myB.cpp, then use: object.function() or
reference->function() syntax to call the function.

To be easier to understand a class for a C programmer, you could treat a C++
class like C struct in your beginning tour of C++, actually struct in C++ is
a special class, i.e. everything in a struct is public.

JT

Victor Bazarov said:
jt said:
Being a newbie in C++ and comming from C, I can't find information on how
to
access a class member function outside its class.
[...]

I don't know what book you're reading that doesn't talk about member
access, but in short you need an object (an instance) of that class
with which you call your member function. The object can be identified
by a variable, by a reference, or by a pointer. For the latter you use
the -> operator to get to a member, for the former two you use the 'dot'
(just like in C). How do you find a member of a struct in C? Have you
ever used a struct in C? Well, that's basically the same thing. Only
if your member is a function, you need to follow its name with the list
of arguments in parentheses, just like with any other function.

class A {
int i;
public:
A(int i_) : i(i_) {}
int increment(int a) { return i += a; }
int decrement(int a) { return i -= a; }
};

int main() {
A a(42);
a.increment(77);
A& ra = a;
ra.decrement(33);
A *pa = &a;
pa->increment(100);
}

V
 
J

jt

class A {
Just like C, in myB.cpp, you should include declaration of class A, and have
an object or reference of class A in myB.cpp, then use: object.function() or
reference->function() syntax to call the function.

Can you please give me an example?

Thank you,
JT
 
D

David White

modemer said:
actually struct in C++ is
a special class, i.e. everything in a struct is public.

Everything public is probably the convention, but a struct in C++ is exactly
the same as a class apart from the default access rights for members and
base classes. A struct can have derived classes, base classes, private
members, virtual member functions and everything else a class can have.

DW
 
M

modemer

example:

// File: lib.h
class A{
public:
void func();
};

// File: lib.cpp
void A::func() {
//do something;
}

// File: main.cpp
#include "lib.h"

int main() {
A a;
a.func();
}

build:
g++ lib.cpp main.cpp
 
G

grahamo

Why didn't anybody mention static functions here? By declaring the
function static, you can invoke a member function without an instance
of the class. There's various rules and constraints that you may need
to take into consideration by doing that, but it will allow you to get
your hands on the function in question without an instance of the
class.


GrahamO
 
D

David White

grahamo said:
Why didn't anybody mention static functions here? By declaring the
function static, you can invoke a member function without an instance
of the class. There's various rules and constraints that you may need
to take into consideration by doing that, but it will allow you to get
your hands on the function in question without an instance of the
class.

Because the function the OP wants to call is not static and, therefore, is
very unlikely to be able to be static, and because static/non-static doesn't
appear to be relevant to the question asked.

DW
 
G

grahamo

s'pose that's fair enough alright :) in general he might be interested
in knowing about them, all the same.
 
J

jt

Static worked like a champ!

Thanks GrahamO

JT

grahamo said:
Why didn't anybody mention static functions here? By declaring the
function static, you can invoke a member function without an instance
of the class. There's various rules and constraints that you may need
to take into consideration by doing that, but it will allow you to get
your hands on the function in question without an instance of the
class.


GrahamO

"David White" <[email protected]> wrote in message
 
H

Howard

jt said:
Static worked like a champ!

Thanks GrahamO

Be careful, there. While this may allow you to call the function without an
instance of the class existing, it may not be what you want. From the type
of code I see in your original post, I think it's more likely that you just
want to use an instance of the form that is created in your code than to
make a call to a static function. Now, if the data you're operating on is
static class data, then I could be wrong, but you haven't shown the contents
of that function, so there's no way for me to know for sure. But I do know
that a CFormView is an MFC class, and that it's part of the document/view
architecture. As such, you'd be far better off asking about how to use it
properly in an mfc newsgroup than here. Also, the online help has a lot of
information about using forms, and a Google search would also turn up a lot
of example code using them.

-Howard
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top