2012-04-21 129 views
0

让我们在主说()我创建了一个名为TheBuilder类名为BOB的对象指针,我这样做是这样的如何将指向某个对象的指针传递给函数? C++

TheBuilder *BOB = new TheBuilder(); 

现在可以说我有,我想这个传递给名为helloWorld功能主要内部。如何将此BOB指针传递给TheBuilder对象?我如何调用它,以及helloWorld()的参数列表是什么样的?

我希望能够修改BOB指向成员函数helloWorld内部的对象内部的数据。

我知道它可能有一些'*'或'&',但我不知道把它们放在哪里。

由于

+4

考虑一个C++教程。 – 2012-04-21 04:54:32

+1

^aha ..来吧..让我们来帮助这个家伙。我曾经在他的位置上,并且确实必须读一本书来弄清楚。但是,直到我在一个使用stackoverflow的人问起时,它仍然感到困惑!谁知道今天他可能终于知道什么时候给你使用什么,如果我们给他一个短暂的贷款手!毕竟他只是要求一个快速的解释,所以他可以做出正确的决定,使用哪一个:D – Pavan 2012-04-21 04:57:55

+1

这是一个太基本的问题。我建议你阅读关于C++的简短教程,你会发现所有的答案,最有可能的是第二或第三段。 – 2012-04-21 04:58:09

回答

0
class TheBuilder 
{ 
    <class def> 
} 

void helloWorld (TheBuilder* obj) 
{ 
    <do stuff to obj, which points at BOB>; 
} 

int main() 
{ 
    TheBuilder *BOB = new TheBuilder(); 
    helloWorld (BOB); 
    return 0; 
} 
+0

哎呦,user1347439打我吧 – lxop 2012-04-21 04:59:48

+0

谢谢,我早些时候尝试过,但忘了在我的原型中做void helloWorld(TheBuilder *),因为我在main之后有了这个函数。 – JDN 2012-04-21 05:03:16

0
void doSomething1(int x){ 
    //code 
} 

这一个经过值的变量,在函数内部无论发生什么情况,原始变量不改变

void doSomething2(int *x){ 
    //code 
} 

在这里传递类型的指针的变量为整数。所以访问号码时,你应该使用的值或x的地址* X

void doSomething3(int &x){ 
    //code 
} 

这就像第一个,但无论发生什么功能里面,原来的变量将被改变,以及

所以你的情况,你会写

void helloWorld (TheBuilder* object){ 
    //Do stuff with BOB 
} 

void main(){ 
    TheBuilder *BOB = new TheBuilder(); 
    helloWorld (BOB); 
} 
+0

什么会调用每个函数的样子?我明显知道价值,但其他两个呢? – JDN 2012-04-21 04:58:23

+0

void helloWorld(TheBuilder * object){// code} and void helloWorld(TheBuilder&object){// code} 这一切都取决于您想要发生的事情。请参阅针对每种情况的个别说明,以告诉您无论使用&或*号,变量会发生什么情况。 – Pavan 2012-04-21 05:03:14

+0

并称他们......它与第一个相同。您可以简单地将值传入,并且在实例通过时其余部分将在方法内处理。你仍然称它为helloWorld(BOB)。很显然,如果你有三个接受相同的实例,但基于*和&作为参数的行为不同,那么方法名称应该不同。 – Pavan 2012-04-21 05:05:23

0

参数列表将包括TheBuilder *参数。例如:

void helloWorld(TheBuilder *theBuilder) { ... }

,并呼吁它:

helloWorld(BOB);

2
int main() 
{ 
    TheBuilder *BOB = new TheBuilder(); 
    helloWorld(BOB); 
    if (BOB->canWeBuildIt) 
     printf("yes we can!"); 
    delete BOB; 
    return 0; 
} 

void helloWorld(TheBuilder *bob) 
{ 
    bob->canWeBuildIt = true; 
} 
+1

这很有趣,谢谢哈哈 – JDN 2012-04-21 05:23:31

0

如果helloWorld是一个成员函数,那么你并不需要通过任何对象的指针:有一个隐式指针,名为this,它指向您调用成员函数的对象:

#include <iostream> 

class TheBuilder 
{ 
    public: 
    void helloWorld(); 

    std::string name_; 
}; 

void TheBuilder::helloWorld() 
{ 
    //Here, you can use "this" to refer to the object on which 
    //"helloWorld" is invoked 

    std::cout << this->name_ << " says hi!\n"; 

    //In fact, you can even omit the "this" and directly use member variables 
    //and member functions 

    std::cout << name << " says hi!\n"; 
} 

int main() 
{ 
    TheBuilder alice; //constructs an object TheBuilder named alice 
    alice.name_ = "Alice"; 

    TheBuilder bob; //constructs an object TheBuilder named bob 
    bob.name_ = "Bob"; 

    alice.helloWorld(); //In helloWorld, "this" points to alice 
         //prints "Alice says hi!" 

    bob.helloWorld(); //In helloWorld, "this" points to bob 
         //prints "Bob says hi!" 
} 

实际上,成员函数几乎就像一个自由函数,其中一个隐式参数对应于被操作的对象。当你写下面的代码:

struct MyClass 
{ 
    int myMemberFunction() {...} 
}; 

MyClass obj; 
obj.myMemberFunction(); 

由编译器生成的代码等同于以下内容:

int myMemberFunction(MyClass * this) {...} 

MyClass obj; 
myMemberFunction(&obj); 

但你并不需要理会这一点。所有你需要明白的是,在对象上调用的成员函数(面向对象术语中的方法),并且操纵它被调用的对象(例如,它可以修改它的成员变量(字段))。在成员函数中,您可以直接使用其名称来访问当前对象成员,也可以使用指针来显式指定对象成员,并使用this指针。