473,320 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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
13 2697
"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
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
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

"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
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
"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
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
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
"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
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
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: ding feng | last post by:
I am a beginner. So this question could be very stupid. Would anyone help me to solve this problem? A formatted txt file is read. Then i need to look into a vector who is a member of a class to...
2
by: diadia | last post by:
when i write two classes as follow . compiler tell me that i can't access private number from other class but if types of two classes are the same it don't have error message ? why? class...
12
by: Bryan Parkoff | last post by:
CMain Class is the base class that is initialized in main function. CA Class is the base class that is initialized in CMain::CMain(). CMain Class is always public while CA Class is always...
42
by: Dan | last post by:
Hello, I have trouble with class calling. I am calling getvolume() with succes in the function CreateCircle but it do not want to call it in ShowCircle() function. I am staying in the same...
5
by: Aakash Joshi | last post by:
is there any way we can access private member function of a class from outside. i know we can access private member data using void pointer
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
5
by: Felix I. Wyss | last post by:
Good Afternoon, I recently noticed that some very simple methods of a template declared and used in a DLL library get inlined when used by the DLL itself, but not by other DLLs and EXEs. After...
1
by: ypjofficial | last post by:
Hi all, what's difference does it make when we define the member function inside and outside a class? I am using vc7. when i define the member functions outside a class with the scope...
7
by: Valeriu Catina | last post by:
Hi, consider the Shape class from the FAQ: class Shape{ public: Shape(); virtual ~Shape(); virtual void draw() = 0;
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.