在C对象数组困惑++ [英] Confused with object arrays in C++
问题描述
所以,我第一次学习Java的,现在我想切换到C ++。我有越来越阵列正常工作有点困难。
So I first learned Java and now I'm trying to switch over to C++. I'm having a little difficulty getting arrays to work correctly.
现在我只是想创建的对象的玩家的数组和一个填充它。但是,我得到一个错误。
Right now I am simply trying to create an array of object "Player" and populate it with one. But I get an error.
Player* players = new Player[1];
players[0] = new Player(playerWidth, playerHeight, 20, 1);
错误说:
操作数=匹配这些操作数。操作数类型是:玩家=玩家*
The error says: the operand "=" matches these operands. Operand types are: Player = Player *
我不明白为什么不起作用?
I can't understand why this doesn't work?
推荐答案
什么错误的意思是,你正在试图分配错误类型的变量的值。当错误说播放器=玩家*
这意味着,在左边的变量是播放器
和值右手边是一个播放器*
。
What the error is saying is that you are trying to assign a value of the wrong type to the variable. When the error says Player = Player *
that means that the variable on the left hand side is a Player
and the value on the right hand side is a Player *
.
players[0] = new Player(playerWidth, playerHeight, 20, 1);
问题是相似的,如果你做的:
The problem is similar to if you were to do:
int x;
x = "Hello, World!";
左,右手类型不匹配,而且也没有自然的转换,使你得到一个错误。
The left and right hand types don't match, and there's no natural conversion, so you get an error.
第一个问题是,你从Java背景的,和Java使用指针很多,但你从他们隐藏。 C ++不隐藏它们。其结果是,C ++有用于与指针明确地处理不同的语法。 Java的摆脱这一切,大多是从C中使用的常规,非指针++的语法处理指针。
The first problem is that you're coming from a Java background, and Java uses pointers a lot but hides them from you. C++ doesn't hide them at all. The consequence is that C++ has different syntax for explicitly dealing with pointers. Java got rid of all that and mostly used the regular, non-pointer syntax from C++ for dealing with pointers.
Java: C++:
Player player = new Player(); Player *player = new Player();
Player player2; Player *player2 = nullptr;
** no equivilant in java ** Player player3;
player.foo(); player->foo();
** no equivilant in java ** player3.foo();
** no equivilant in java ** *player;
** no equivilant in java ** &player2;
要了解使用指针工作,并直接与工作对象之间的区别是非常重要的:
It's very important to understand the difference between working with pointers and working directly with an object:
Java: C++:
Player a = new Player(); Player *a = new Player();
Player b = a; Player *b = a;
b.foo(); b->foo();
在此code有只有一个对象,你可以通过两种 A
或 B
它不会有所作为, A
和 b
都指向同一个对象。
In this code there's only a single object, and you can access it through either a
or b
and it doesn't make a difference, a
and b
are both pointers to the same object.
C++:
Player c = Player();
Player d = c;
d.foo();
在此code有两个对象。它们是不同的,并且做一些 D
不影响 C
。
In this code there are two objects. They are distinct, and doing something to d
does not affect c
.
如果在Java中,您了解了像 INT
对象类型原始类型之间的区别,如字符串
然后想想它的一种方法是,在C ++中的所有对象是原始的。如果我们在您的code回过头来,用这个C ++对象像Java的原始'的规则你也许可以看得更清楚什么是错的:
If in Java you learned about the distinction between 'primitive' types like int
and Object types like String
then one way to think about it is that in C++ all objects are primitive. If we look back at your code and use this 'C++ objects are like Java primitives' rule you can maybe see better what's wrong:
Java:
int[] players = new int[1];
players[0] = new int(playerWidth); // huh???
这应该说清楚,转让的右边应当仅仅是一个球员的价值,而不是一个新的球员对象的动态分配。在Java中一个int这看起来像玩家[0] = 100;
。由于Java Object类型是不同的Java没有办法写对象的值的你可以写 INT
值的方式。但是C ++做的事情; 玩家[0] =播放器(playerWidth,playerHeight,20,1);
That should make it clear that the right hand side of the assignment should simply be a Player value rather than a dynamic allocation of a new player object. For an int in java this looks like players[0] = 100;
. Since Object types in Java are different Java doesn't have a way to write Object values the way you can write int
values. But C++ does; players[0] = Player(playerWidth, playerHeight, 20, 1);
第二个问题是,在C数组是怪异和C ++继承了。
The second problem is that arrays in C are weird and C++ inherited that.
在C和C ++的指针允许'指针运算。如果你有一个指向对象,你可以添加或减去它并获得一个指向不同的对象。 Java有什么与此类似。
Pointers in C and C++ allow 'pointer arithmetic. If you have a pointer to an object you can add to or subtract from it and get a pointer to a different object. Java has nothing similar to this.
int x[2]; // create an array of two ints, the ints are 'adjacent' to one another
// if you take the address for the first one and 'increment' it
// then you'll have a pointer to the second one.
int *i = &x[0]; // i is a pointer to the first element
int *j = &x[1]; // j is a pointer to the second element
// i + 1 equals j
// i equals j - 1
此外数组索引运算符 []
适用于指针。 X [5]
等同于 *(X + 5)
。这意味着,指针可以用作阵列,这是惯用的和预期的用C和C ++。事实上它甚至烤成C ++。
Additionally the array index operator []
works on pointers. x[5]
is equivalent to *(x+5)
. This means that pointers can be used as arrays, and that is idiomatic and expected in C and C++. In fact it's even baked into C++.
在C ++中,当您使用新
来动态分配一个对象,例如新播放器
,你通常会得到一个指向指定类型。在这个例子中,你得到播放器*
。但是,当你动态分配一个数组,例如新的播放器[5]
,它是不同的。相反取回一个指针五玩家一个数组
,你实际上得到一个指向第一个元素。这就像任何其他播放器*
:
In C++ when you use new
to dynamically allocate an object, e.g. new Player
, you normally get a pointer to the type you specified. In this example you get Player *
. But when you dynamically allocate an array, e.g. new Player[5]
, it's different. Instead of getting back a pointer to an array of five Players
, you actually get back a pointer to the first element. This is just like any other Player *
:
Player *p = new Player; // not an array
Player *arr = new Player[5]; // an array
这使得该指针唯一不同的是,当你做就可以了指针算法你指向有效的播放器
对象:
The only thing that makes this pointer different is that when you do pointer arithmetic on it you get pointers to valid Player
objects:
Player *x = p + 1; // not pointing at a valid Player
Player *y = arr + 3; // pointing at the fourth array element
新
和删除
是很难,如果你使用它们没有保护正确使用。为了证明这一点:
new
and delete
are hard to use correctly if you use them without protection. To demonstrate this:
int *x = new int;
foo();
delete x;
这code是容易出错,可能是错的。特别是,如果富()
抛出一个异常,那么 X
泄漏。
This code is error prone and probably wrong. Specifically, if foo()
throws an exception then x
is leaked.
在C ++中,每当你获得一种责任,这样当你调用为新
您获得的责任调用删除
在稍后的时间,你应该记住
In C++ whenever you acquire a responsibility, such as when you call new
you acquire the responsibility to call delete
at a later time, you should remember
R.A.I.I。
责任*获取就是初始化
R.A.I.I.
Responsibility* Acquisition Is Initialization
<子> *更频繁的人说'资源获取就是初始化,但资源只有一种责任。我被说服使用由乔恩·卡尔布后者长期在他的异常安全的C ++ 谈判之一。子>
* More frequently people say 'resource acquisition is initialization', but resources are only one kind of responsibility. I was persuaded to use the latter term by Jon Kalb in one of his Exception Safe C++ talks.
R.A.I.I。也就是说,只要你获得一种责任,它应该看起来像你初始化的对象;特别是你在初始化一个特殊的对象是谁的目的是管理你的责任。这种类型的一个例子是的std ::的unique_ptr&LT; INT&GT;
将管理指针 INT
s的<分配code>新:
R.A.I.I. means that whenever you acquire a responsibility, it should look like you're initializing an object; specifically you're initializing a special object who's purpose is to manage that responsibility for you. One example of such an type is std::unique_ptr<int>
which will manage pointers to int
s allocated with new
:
C++:
std::unique_ptr<int> x(new int);
foo();
// no 'delete x;'
要管理你的播放器
数组你会使用的std :: unqiue_ptr
是这样的:
To manage your Player
array you'd use std::unqiue_ptr
like this:
std::unique_ptr<Player[]> players(new Player[1]);
players[0] = Player(playerWidth, playerHeight, 20, 1);
现在的的unique_ptr
将处理你,分配和你不需要调用删除
自己。 (注:当你分配一个数组,你应该给的unique_ptr
一个数组类型; 的std ::的unique_ptr&LT;播放器[]&GT;
,当你分配其他任何你使用非数组类型,的std ::的unique_ptr&LT;播放方式&gt;
)
Now the unique_ptr
will handle that allocation for you and you don't need to call delete
yourself. (N.B. when you allocate an array you should give unique_ptr
an array type; std::unique_ptr<Player[]>
, and when you allocate anything else you use a non-array type, std::unique_ptr<Player>
.)
++有一个更加专业化的R.A.I.I.当然的C类型管理阵列,的std ::矢量
,你应该preFER,要使用的std ::的unique_ptr
Of course C++ has an even more specialized R.A.I.I. type for managing arrays, std::vector
, and you should prefer that to using std::unique_ptr
:
std::vector<Player> players(1);
players[0] = Player(playerWidth, playerHeight, 20, 1);
还是在C ++ 11:
Or in C++11:
std::vector<Player> players { Player(playerWidth, playerHeight, 20, 1) };
这篇关于在C对象数组困惑++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!