且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在类之外访问类成员函数?

更新时间:2023-02-15 18:34:22

" 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" &LT;五******** @ comAcast.net&GT;在消息中写道

新闻:DN ******************** @ comcast.com ..." jt" &LT; JT **** @ hotmail.com&GT;写了...
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