2013-10-20 25 views
0

所以我有2个函数和1个类。带有1个函数的 我想设置存储在类中的整数的值。与其他函数 我想再次使用这些值。 我正在使用指针,因为我认为这将保存在整个程序的内存地址上。在多个函数中使用类中的指针C++

#include <iostream> 
using namespace std; 


void Function1(); 
void Function2(); 
class TestClass 
{ 
public: 
    TestClass(); 
    ~TestClass(); 
    void SetValue(int localValue) 
    { 
     *value = localvalue; 
    } 
    int GetValue()const 
    { 
     return *value; 
    } 
private: 
    *value; 
}; 

TestClass::TestClass() 
{ 
    value = new int(0); 
} 

TestClass: 
~TestClass() 
{ 
    delete value; 
} 

int main() 
{ 
    TestClass *tommy = new TestClass; //this didn't work, 
    //couldn't use SetValue or Getvalue in functions 
    Function1(); 
    Function2(); 
    return 0; 
} 

void Function1() 
{ 
    int randomvalue = 2; 
    TestClass *tommy = new TestClass; //because it didnt work in main, i've put it here 
    tommy->SetValue(randomvalue); 
} 

void Function2() 
{ 
    TestClass *tommy = new TestClass; 
    cout << tommy->GetValue(); 
      << endl; //this gave a error, so I put the above in again 
    //but this returns 0, so the value isn't changed 
} 

那么,给我一个解决方案?我没有收到任何编译错误,但值没有改变,可能是因为在Function1完成后调用了析构函数。那我该怎么做呢?

+0

请更具体一些。什么没有工作?您是否收到错误消息(如果是这样,请发布)?你有没有得到意想不到的行为(如果是这样,描述它)? –

+0

你有没有听过缩进。使事情易读 –

回答

1

您需要从main()通过你的tommy到您的每一个功能,不会造成每次在一个新的,否则你只是失去你创建你的函数的新Testclass对象,而实际上这里因为使用new而导致内存泄漏。

喜欢的东西:

void Function1(TestClass * tommy) { 
    int randomvalue =2; 
    tommy->SetValue(randomvalue); 
} 

,然后在main()

int main() { 
    TestClass *tommy = new TestClass; 
    Function1(tommy); 
    std::cout << tommy->GetValue() << std::endl; // Outputs 2 
    delete tommy; 
    return 0; 
} 

这是一个奇怪的使用情况下,虽然 - 这将是你所期望的成员函数做那种事。这将是更好的:

int main() { 
    TestClass *tommy = new TestClass; 
    tommy->SetValue(2); 
    std::cout << tommy->GetValue() << std::endl; // Outputs 2 
    delete tommy; 
    return 0; 
} 

无需Function1()Function2()。无论哪种方式,你将不得不修复:

private: 
*value; 

在你的班级,正如别人指出的那样。

+0

那么,为什么在这种情况下使用指针呢? – Chad

+0

@查德:为什么呢?询问OP,他在他的问题中说他想使用它们。尽管如此,我并不特别同意指针总是要避免的,但是如果没有看到真正的用例(意思是我怀疑这是他想写的程序的全部内容),那么关于形成关于他们在这里是否最好的观点。 –

+0

我意识到,SO是关于回答直接问题的,但是这个问题显然是来自对C++没有多少经验的人。他使用指针是因为他“认为这将被保存在内存中”。我们应该努力不只是让“有效的代码”,而是“更好的”代码。 – Chad

0

每次您编写new TestClass时,您都会完全创建一个TestClass对象的新实例。新实例与任何现有实例都无关,除了具有相同的类型。要使TestClass的单个实例成为您的函数所使用的“唯一”实例,您需要将它作为参数传递给这些函数。

此外 - 不要使用指针,除非它是绝对必要的

下面是一个清理完成的代码示例,它完成了您尝试的内容。

class TestClass 
{ 
    int value; 

public: 
    TestClass() : value(0) 
    {} 

    int GetValue() const { return value; } 
    void SetValue(int x) { value = x; } 
}; 

// takes a "reference", works somewhat like a pointer but with 
// some additional safety guarantees (most likely will not be null) 
// This will modify the original passed in TestClass instance. 
void SetRandomValue(TestClass& tc) 
{ 
    int random = 2; // not really random... 
    tc.SetValue(random); 
} 

// take a const reference, similar to above comment, but a const 
// reference cannot be modified in this scope 
void Print(const TestClass& tc) 
{ 
    std::cout << tc.GetValue() << "\n"; 
} 

int main() 
{ 
    // create a TestClass instance with automatic storage duration 
    // no need to use a pointer, or dynamic allocation 
    TestClass tc; 

    // Modify the instance (using reference) 
    SetRandomValue(tc); 

    // print the instance (using const reference)  
    Print(tc); 

    return 0; 
} 
+0

为什么这是downvoted?这是完全有效的,很好的建议。 – acron

+0

这是不是我downvoted,但这不回答OP为什么他不得不写'新的TestClass'三次。 –

+0

@Agent_L,够了。编辑。 – Chad

0

您没有将您的TestClass传递给任一函数,因此它们的函数无法看到您创建的tommy对象。然后在每个函数中创建一个新的局部变量,它恰好与您的本地变量名称相同...它们都是独立对象

+0

啊,当我回答这个问题时,下面有人用代码写了同样的东西。选择他! –