如何访问对象的成员变量的取消引用值

How to access object's member variable's dereferenced value

本文关键字:成员 变量 引用 取消 对象 何访问 访问      更新时间:2023-10-16

我正在尝试复制一个传递给复制构造函数的对象。我想访问传递给此函数的对象的成员变量的取消引用值,但在'('token int*c=new int(other.(*pa((;之前收到错误"预期的非限定id">

类别定义为:

class Foo {
Public:
int *a, *b;
Foo(const Foo &); //copy constructor
}

我的功能已定义:

Foo::Foo(const Foo& other) {
int* c = new int(other.(*a));
int* d = new int(other.(*b));
}

主要定义为:

Foo first(1,2);
Foo second(first); 

复制构造函数可以看起来像

Foo::Foo(const Foo& other) : a( new int( *other.a ) ), b( new int( *other.b ) )
{
}

这是一个演示程序

#include <iostream>
class Foo {
public:
int *a, *b;
Foo( int x, int y ) : a( new int( x ) ), b( new int( y ) )
{
}
Foo( const Foo &other  ) : a( new int( *other.a ) ), b( new int( *other.b ) )
{
}
};
int main() 
{
Foo first(1,2);
Foo second(first); 
std::cout << *first.a << ", " << *first.b << 'n';
std::cout << *second.a << ", " << *second.b << 'n';
return 0;
}

其输出为

1, 2
1, 2

所有其他特殊的成员函数,例如析构函数,我希望你能定义自己。

将值分配给对象成员。

Foo::Foo(const Foo& other) {
this->a = new int(other.(*a));
this->b = new int(other.(*b));
}