By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,350 Members | 1,209 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,350 IT Pros & Developers. It's quick & easy.

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

P: n/a
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


Jul 23 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
"jt" <jt****@hotmail.com> wrote...
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
Jul 23 '05 #2

P: n/a
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" <v.********@comAcast.net> wrote in message
news:DN********************@comcast.com... "jt" <jt****@hotmail.com> wrote...
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

Jul 23 '05 #3

P: n/a
jt schrieb:
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
Jul 23 '05 #4

P: n/a

"jt" <jt****@hotmail.com>
:tj*********************@tornado.tampabay.rr.com.. .
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?


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" <v.********@comAcast.net> wrote in message
news:DN********************@comcast.com...
"jt" <jt****@hotmail.com> wrote...
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


Jul 23 '05 #5

P: n/a
jt
> > > 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?


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

Jul 23 '05 #6

P: n/a
"modemer" <mo*****@yahoo.com> wrote in message
news:d1**********@domitilla.aioe.org...

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
Jul 23 '05 #7

P: n/a
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
Jul 23 '05 #8

P: n/a
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" <no@email.provided> wrote in message news:<QD******************@nasal.pacific.net.au>.. .
"modemer" <mo*****@yahoo.com> wrote in message
news:d1**********@domitilla.aioe.org...

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

Jul 23 '05 #9

P: n/a
"grahamo" <gr************@hotmail.com> wrote in message
news:79**************************@posting.google.c om...
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
Jul 23 '05 #10

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


"David White" <no@email.provided> wrote in message news:<r8******************@nasal.pacific.net.au>.. .
"grahamo" <gr************@hotmail.com> wrote in message
news:79**************************@posting.google.c om...
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

Jul 23 '05 #11

P: n/a
jt
Static worked like a champ!

Thanks GrahamO

JT

"grahamo" <gr************@hotmail.com> wrote in message
news:79**************************@posting.google.c om...
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" <no@email.provided> wrote in message

news:<QD******************@nasal.pacific.net.au>.. .
"modemer" <mo*****@yahoo.com> wrote in message
news:d1**********@domitilla.aioe.org...

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

Jul 23 '05 #12

P: n/a

"jt" <jt****@hotmail.com> wrote in message
news:_Z*********************@tornado.tampabay.rr.c om...
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
Jul 23 '05 #13

P: n/a
No worries JT. You've made my day with that response :)

"jt" <jt****@hotmail.com> wrote in message news:<_Z*********************@tornado.tampabay.rr. com>...
Static worked like a champ!

Thanks GrahamO

JT

"grahamo" <gr************@hotmail.com> wrote in message
news:79**************************@posting.google.c om...
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" <no@email.provided> wrote in message

news:<QD******************@nasal.pacific.net.au>.. .
"modemer" <mo*****@yahoo.com> wrote in message
news:d1**********@domitilla.aioe.org...
>
> 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

Jul 23 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.