如何在类之外访问类成员函数? [英] How to access a class member function outside of its class?

查看:108
本文介绍了如何在类之外访问类成员函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为C ++的新手并且来自C,我无法找到有关如何在课堂外访问类成员函数的信息。


以下是该类及其成员函数的片段:查看

AddCallPages

============== ==================================== ===========
class CPsnstatuspttView:public CFormView

{

protected://仅从序列化创建

CPsnstatuspttView();

DECLARE_DYNCREATE(CPsnstatuspttView)


public:

// {{AFX_DATA(CPsnstatuspttView)

enum {IDD = IDD_PSNSTATUSPTT_FORM};

CEdit m_edit_msgs_ctrl;

CString m_edit_messages;

//}} AFX_DATA


//属性

public:

CPsnstatuspttDoc * GetDocument();


//操作

public:


//覆盖

// ClassWizard生成的虚函数覆盖

// {{AFX_VIRTUAL(CPsnstatuspttView)

public:

virtual BOOL PreCreateWindow(CREATESTRUCT& cs);

protected:

virtual void DoDataExchange(CDataExchange * pDX); // DDX / DDV支持

virtual void OnInitialUpdate(); //构造后第一次调用

//}} AFX_VIRTUAL


//实施

public:


CString m_ConnectPort;

CString m_ConnectHost;


void AddCallPages(void); ..< -------------------------这个函数我

想从另一个模块调用

============================================= ===== =======================

或者有更简单的方法可以做到这一点在课外可能吗?


感谢您的帮助。

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


推荐答案

" jt" < JT **** @ hotmail.com>写了...
"jt" <jt****@hotmail.com> wrote...
作为C ++的新手,从C来,我找不到如何
访问类外的类成员函数的信息。 br /> [...]
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.
[...]




我不知道你正在读什么书而不谈论会员

访问,但简而言之,您需要该类的一个对象(一个实例)

,您可以使用它来调用您的成员函数。可以通过变量,引用或指针来识别对象
。对于后者你使用

- >操作员要找到一个会员,前两个你使用''点''

(就像在C中一样)。你如何在C中找到结构的成员?你有没有在B中使用过
?嗯,这基本上是一回事。只有

如果您的成员是一个函数,您需要在括号中使用列表

参数,就像使用任何其他函数一样。


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



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


对不起,那不是我在问什么你误解了这个问题,或者我没有b $ b没有正确说明。


我已经知道你在下面说的了。

我会再次尝试我的问题。我希望从模块外部调用另一个类中的函数。

使用你的例子说我想调用A.increment,但是在myB中做。 cpp


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
:
A类{
int i;
公开:
A(int i_):i( i_){}
int increment(int a){return i + = a; } int /> int decrement(int a){return i - = a; }
};


现在我有另一个名为myB.cpp的cpp文件:

class B {
int i;
public:
B(int i_):i(i_){}
int add(int a){return i + = a; } int /> int subtract(int a){return i - = a; }
}


在文件myB.cpp中,如何调用类A.increment?


JT


" Victor Bazarov" <五******** @ comAcast.net>在消息中写道

新闻:DN ******************** @ comcast.com ..." jt" < JT **** @ hotmail.com>写了...
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...
作为C ++的新手,从C来,我找不到有关
的信息如何访问类外的类成员函数。
[...]
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.
[...]



我不知道你正在阅读什么书没有谈论会员访问,但简而言之你需要一个对象(一个实例),你可以用它来调用你的成员函数。可以通过变量,引用或指针来识别对象。对于后者你使用
- >操作员要找到一个会员,对于前两个你使用''点''
(就像在C中一样)。你如何在C中找到结构的成员?你有没有在C中使用过结构?嗯,这基本上是一回事。只有
如果你的成员是一个函数,你需要在括号中使用参数列表,就像任何其他函数一样。

A类{\\ br /> int i;
public:
A(int i_):i(i_){}
int increment(int a){return i + = a; } int /> 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



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



jt schrieb:
jt schrieb:
对不起,那不是什么我在问。你误解了这个问题,或者我没有正确说明。

我已经知道你在下面说了什么。

我会再试一次我的问题。我希望从模块外部调用另一个类中的函数。
使用你的例子说我想调用A.increment,但是在myB.cpp中执行
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




几乎和你在C中一样。在myA.h中声明A类(比如在C中用

原型)并在myA.cpp中定义它。然后在#b
myA.cpp中使用#include myA.h(因此编译器在看到

定义时已知该声明)和myB.cpp(因此编译器)了解A级课程:


myA.h:


#ifndef MYA_H

#define MYA_H


A级
{

int i;

public:

A(int);

int increment(int);

int decrement(int);

};

#endif


myA.cpp:


A :: A(int i_)

:我(i_)

{

}


int A :: increment(int a)

{

返回i + = 1;

}


int A ::减量(int a)

{

返回i - = 1;

}


myB.cpp:


int main()

{

A a(42);

a.increment(666);

}


当然这些琐碎的函数是内联定义的候选者

在标题内,只是让你明白了。

myA.h现在有点类似于这样的C标题:


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 ::减量(int)* /


干杯,

Malte



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


这篇关于如何在类之外访问类成员函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆