bad_cast如果你转换引用,dynamic_cast会抛出异常,但正如我所知,标准指针被认为是引用,即指针是引用的一种类型.
那么在投射指针时我应该得到bad_cast吗?
这个问题来自这个页面的try-catch块.这个try-catch块不合适吗?
这是删除使用new创建的longs和object的地图的好方法
// iterate over the map
for (std::map<unsigned long, Object*>::iterator it = objects.begin(), it_end = objects.end(); it != it_end; ++it)
{
Object* temp = it->second;
if(temp)
delete temp;
}
// clear the map
objects.clear();
Run Code Online (Sandbox Code Playgroud) 在C++中,我遇到指针等问题.如何解决以下问题?
错误:''='中的'operator ='不匹配(stage-> Stage :: tiles +((unsigned int)(((unsigned int)t)*12u)))=(operator new(12u),(,((瓷砖*))))"| 注意:候选人是:Tile&Tile :: operator =(const Tile&)|*
stage.h
#include "Tile.h"
class Stage {
public:
Tile *tiles;
int size;
void init(int size);
};
Run Code Online (Sandbox Code Playgroud)
stage.cpp
void Stage::init(int size) {
this->size = size;
this->tiles = new Tile[size];
}
Run Code Online (Sandbox Code Playgroud)
application.cpp
#include "Stage.h"
#include "Tile.h"
bool setTiles( Stage * stage ) {
for( int t = 0; t < stage->size; t++ ) {
stage->tiles[t] = new Tile();
}
return true;
}
stage.init(1234);
setTiles( &stage );
Run Code Online (Sandbox Code Playgroud)
另外,我真的不知道何时使用 …
我对指针数组有点困惑,我只是想确保我是对的.
当我写int *arr它时,它只是一个指向int变量的指针,而不是一个数组.只是我初始化它(比如说malloc)它变成了一个数组.我到目前为止对吗?
另外我还有另外一个问题:给了(在学校里)一个小功能,应该返回一系列成绩,第一个单元格是平均值.这个功能是刻意错误的:他们所做的就是设定
int *grades = getAllGrades();
Run Code Online (Sandbox Code Playgroud)
而且他们已经将指针减少了一个用于普通'细胞'
*(grades - 1) = getAverage();
return *(grades - 1)
Run Code Online (Sandbox Code Playgroud)
我知道这是错误的,因为返回的值不是数组,我只是不知道如何解释它.当我设置指针时,机器/编译器如何知道我是否只需要指针或数组?
(如果我不清楚它,因为我试图询问一些对我来说仍然含糊不清的事情,我道歉)
我尝试使用类中的方法和this指针将指针复制到另一个指针,如下所示.我给出了完整的测试代码,以便明确发生了什么.
class test {
private:
int x;
public:
void setx(int x);
int getx(void);
void copy(test *temp);
};
void test::setx(int x) {
this->x = x;
}
int test::getx(void) {
return this->x;
}
void test::copy(test *temp) {
this = temp;
}
Run Code Online (Sandbox Code Playgroud)
我从主要访问此方法如下:
int main() {
test a;
a.setx(4);
cout << a.getx()<<endl;
test *b = new test;
b->setx(4);
cout << b->getx()<<endl;
test *c;
c=b;
cout << c->getx()<<endl;
test *d;
d->copy(b);
cout << d->getx()<<endl;
}
Run Code Online (Sandbox Code Playgroud)
但是它会出现以下错误
In member function ‘void test::copy(test*)’:
error: …Run Code Online (Sandbox Code Playgroud) 我正在学习Objective C来在iOS上编程.我知道它与指针有关,我无法理解.我不知道创建一个NSObject像这样的字符串(或任何)的区别是什么:
NSString place = ...;
or
NSString *place = ...;
Run Code Online (Sandbox Code Playgroud)
谢谢您的帮助!!
我只是想知道这是否是C89程序的"好"代码.
obj_ptr = (obj*) (ptr1 || ptr2);
Run Code Online (Sandbox Code Playgroud)
基本上它的作用(至少在我的计算机上的GCC中)是将obj_ptr设置为ptr1,如果ptr1!= NULL,则为ptr2.
我环顾四周,看不出这是否正确,但从||的事实判断 运算符必须将指针转换为整数然后我必须将它们抛回来是一种不好的风格.
如果这是不好的风格或不可移植,并且是否有更好的(希望)同样简洁的解决方案?
编辑:我主要关心的是我编写的代码是否可移植,并且不依赖于未定义的行为.
我可能找到了一种更好的便携方式,我认为它是"好的风格"(除非你不喜欢if语句中的赋值).
if(!(obj_ptr = ptr1))
obj_ptr = ptr2;
Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int width = 100;
int height = 100;
float cam[] = {-1.1,-1.0,1.2};
float D[] = {0.2,0.2,-0.2};
float U[] = {0.0,1.0,0.0};
float* cross(float* v1,float* v2)
{
float el1[] = {(v1[1]*v2[2]-v1[2]*v2[1]),(v1[2]*v2[0]-v1[0]*v2[2]),(v1[0]*v2[1]-v1[1]*v2[0])};
return el1;
}
float* neg3(float* v)
{
float arr[3];
for(int i=0;i<3;i++)
arr[i] = 0.0-(v[i]);
return arr;
}
/*
float* cam_space(float* p)
{
float* e1 = cross(D,U);
float* e2 = cross(D,cross(U,D));
float* e3_n = D;
float ar[4];
ar[0] = e1[0]*p[0]+e1[1]*p[1]+e1[2]*p[2];
ar[1] = e2[0]*p[0]+e2[1]*p[1]+e2[2]*p[2]; …Run Code Online (Sandbox Code Playgroud) 以下程序在初始化时给出了错误(*a)[5]=((*)[])&v;.当我没有输入该行时,我仍然会收到错误.
int main()
{
int v[10];
int **p;
int (*a)[5];
(*a)[5]=((*)[])&v;
printf("%d\n",*a);
printf("%d\n",sizeof(v));
return 0;
}
Run Code Online (Sandbox Code Playgroud) 为什么这段代码会产生意外的输出.
int * func(int *xp)
{
int y = 10 + *xp;
return (&y);
}
void main()
{
int x = 10;
int *xp = func(&x);
printf("%d\n", x);
printf("%d\n", *xp);
}
Run Code Online (Sandbox Code Playgroud)
预期产量:
10
20
Run Code Online (Sandbox Code Playgroud)
实际输出:
10
1074194112
Run Code Online (Sandbox Code Playgroud) pointers ×10
c++ ×6
c ×4
arrays ×2
casting ×2
cocoa-touch ×1
ios ×1
iphone ×1
memory ×1
new-operator ×1
objective-c ×1
reference ×1
this ×1