gpt4 book ai didi

C++ : Assign normal return value to a pointer

转载 作者:行者123 更新时间:2023-11-30 03:07:45 27 4
gpt4 key购买 nike

如何将函数的正常返回值分配给指针?

比如我想给这个static成员函数的返回值赋值:

int AnotherClass::getInt();

在下面的表达式中:

// m_ipA is a private member of the class `Class`
int *m_ipA;
// Lots of things in between, then :
void Class::printOutput() {
m_ipA = AnotherClass::getInt();
// Some operations on m_iPA here, then
// Print instructions here
}

我是否需要在构造函数中使用 new 关键字初始化 m_ipA

提前致谢。

最佳答案

如果 m_ipA 没有指向任何有效的内存位置,那么您需要像下面这样分配内存:

m_ipA = new int(AnotherClass::getInt());

关于C++ : Assign normal return value to a pointer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5620193/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com