我想了解标准关于将指针解引用到基的观点,但是我在寻找它方面没有任何进展。以这两个类为例:
class Base
{
public:
virtual void do_something() = 0;
};
class Derived : public Base
{
public:
virtual void do_something();
};
void foo2(Base *b)
{
Base &b1 = *b; // how this work by standard?
}
void foo()
{
Derived *d = new Derived();
foo2(d); // does this work by standard?
}
Run Code Online (Sandbox Code Playgroud)
因此,基本上,如果取消引用指向类型D的对象的类型B的指针,切片会在适当的位置发生,还是会临时出现?我倾向于认为临时不是一个选择,因为这意味着临时是抽象类的实例。
不管真相如何,我都希望能有一个指向ISO标准的指针。(或者第三,就此而言。:))
编辑:
我提出的观点是,暂时不作为一种选择,因为它可能会以其行为方式行事,这是很合逻辑的,但我无法在标准中找到确认,而且我也不是普通读者。
编辑2:
通过讨论,很明显,我的问题实际上是关于取消引用指针机制的,而不是关于拼接或临时的。我感谢每个人都为我做的愚蠢,最后我得到了最令我困惑的问题的答案:为什么我找不到标准的东西……显然这是错误的问题,但是我有正确的答案。
n
当我们运行这段代码时,它正常工作并string constant在屏幕上打印:
char *someFun(){
char *temp = "string constant";
return temp;
}
int main(){
puts(someFun());
}
Run Code Online (Sandbox Code Playgroud)
但是,当我们运行以下类似的代码时,它将无法工作并在屏幕上打印一些垃圾:
char *someFun1(){
char temp[ ] = "string";
return temp;
}
int main(){
puts(someFun1());
}
Run Code Online (Sandbox Code Playgroud)
它背后的原因是什么?本质上,两个函数都做类似的事情(即返回"字符串"),但它们仍然表现不同.这是为什么?
我正在尝试创建使用指针函数作为参数的代码,我必须使用typedef.我不是C专业人士.感觉就像我到了那里,但我似乎无法找到对函数指针语法的一个很好的解释.
我有一个函数fillArray:
long fillArray(long *array, int x, int y) {
//
}
Run Code Online (Sandbox Code Playgroud)
然后我想创建一个指向此函数的指针的typedef:
typedef long (*fArray)(long, int, int);
fArray pFill = fillArray;
Run Code Online (Sandbox Code Playgroud)
我想将这个pFill赋予一个名为doThis()的函数:
int doThis (fArray pFill) {
return 0;
}
Run Code Online (Sandbox Code Playgroud)
并使用以下方法调用它
int y = doThis(pFill);
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我试图在C中编写一个链表程序,但是我继续从不兼容的指针类型警告/错误中获取初始化.我怎么摆脱这个?你能解释一下是什么问题吗?以下是我的程序的简化版本:
typedef struct node
{
int contents;
struct Node *nextNode;
} Node;
int main(void)
{
//.......Other code here......
Node *rootNode = (Node *) malloc(sizeof(Node));
rootNode->nextNode = NULL;
//.......Other code here......
addNode(rootNode);
}
addNode(Node *currentNode)
{
//.....Other code here....
Node *nextNode = (currentNode->nextNode); //Error on this line
// ....Other code here...
}
Run Code Online (Sandbox Code Playgroud)
谢谢
我在C中创建一个链表程序,我继续得到分段错误.我已经将问题缩小到几行代码,我相信它与检查NULL有关.我怎样才能解决这个问题?这是代码:
typedef struct node
{
int contents;
struct node *nextNode;
} Node;
deleteNode(Node *currentNode)
{
while((currentNode->contents) != NULL)
{
//.....Do Stuff.....
currentNode = currentNode->nextNode;
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢
我对此很新,所以请耐心等待.对于编码C类,我们总共使用九个函数和一个头文件(我们称之为my.h)来收集房间的长度和宽度(int),折扣百分比(也是一个int) ),和地毯的单价(双倍).我们将使用输入来计算安装地毯的总成本.我可以得到打印的长度,宽度和面积,但不是单价.它从Read_Data.c和Calc_Value.c函数打印frin,但不在Install_Calice.c中调用,而是在Calc_Value.c中调用.unit_price与长度和宽度同时读取,但不知何故,它没有正确传递.我不知道为什么.也许它需要一个不同的指针.任何帮助都会非常感激.我已经阅读了我的书,并与我的教授和同学交谈,并在互联网上搜索,但我找不到任何有用的东西.Install_Price的代码是:
/*This function calculates the cost of the carpet and the labor cost in order to
calculate the installed price.*/
#include "my.h"
void Install_Price (int length, int width, int unit_price, int* area,
double* carpet_cost, double* labor_cost, double* installed_price)
{
printf("\nThe unit price is %7.2f.\n", *unit_price);
*area = length * width;
*carpet_cost = (*area) * unit_price;
printf("The carpet cost is %7d x %7.2f = %7.2f.\n", *area, unit_price, *carpet_cost);
*labor_cost = (*area) * LABOR_RATE;
*installed_price = (*carpet_cost) + (*labor_cost);
return;
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试在运行ac程序时了解内存中存储的内容(堆栈/堆,还有其他吗?).编译它会发出警告:函数返回局部变量的地址:
char *giveString (void)
{
char string[] = "Test";
return string;
}
int main (void)
{
char *string = giveString ();
printf ("%s\n", string);
}
Run Code Online (Sandbox Code Playgroud)
跑步给出了各种结果,它只是打印乱码.我从中收集到,在giveString()中调用字符串的char数组在运行时存储在giveString()函数的堆栈帧中.但是,如果我将giveString()中的字符串类型从char数组更改为char指针:
char *string = "Test";
Run Code Online (Sandbox Code Playgroud)
我没有收到警告,程序打印出"测试".那么这是否意味着字符串"Test"现在位于堆上?它当然不会出现在giveString()的堆栈框架中.这两种情况究竟发生了什么?如果这个字符串位于堆上,那么程序的所有部分都可以通过指针访问它,它会在程序终止之前永远不会被释放吗?或者如果没有指向它的指针就会释放内存空间,就好像我没有将指针返回到main?(但这只能用Java中的垃圾收集器来实现,对吧?)这是堆分配的一个特例,它只适用于指向常量字符串(硬编码字符串)的指针吗?
假设我有一个带有两个私有变量的C++类.固定大小的数组,data以及指向该数组的指针pnt.
class MyClass
{
private:
double *pnt;
double data[2];
public:
myClass();
virtual ~MyClass();
double* getPnt() const;
void setPnt(double* input);
};
MyClass::MyClass()
{
double * input;
data[0] = 1;
data[1] = 2;
input= data;
setPnt(input);
}
MyClass::~MyClass()
{
delete this->pnt; // This throws a runtime error
}
void MyClass::setPnt(double * input)
{
pnt = input;
}
double * MyClass::getPnt() const;
{
return pnt;
}
int main()
{
MyClass spam; // Construct object
delete spam; // Error C2440: …Run Code Online (Sandbox Code Playgroud) 我正在构建一个Coldfusion组件的问题.好吧,我不确定这是一个问题,或者事情是如何起作用的,但这看起来很奇怪.首先,我为我的组件的init函数设置参数并调用组件.但是如果我在调用后更改其中一个参数,它也会更改组件中保存的值.它是否正确?
我不确定我是否能很好地解释这一点.这是一些伪代码:
<cfset fruit = [] />
<cfset fruit[1] = {
id = 1,
name = "apple"
} />
<cfset fruit[2] = {
id = 2,
name = "orange"
} />
<cfset args = {
title = "Fruit",
data = fruit
} />
<cfinvoke component="test" method="init" returnvariable="test" argumentcollection=#args# />
<cfset fruit[2].name = "banana" />
Run Code Online (Sandbox Code Playgroud)
该init函数将title和data参数存储在组件的variables范围中.
现在,如果我从test对象输出数据,我得到apple和banana.但我期望该组件保留在调用期间收到的数据.
如果我需要澄清任何事情,请告诉我.谢谢你的帮助!
在以下代码中我收到错误:
a value of type (double*)(const double& arg) const cannot be assigned to an entity of type pt2calculateA
有关如何使其工作的任何建议?
class myClass {
private:
typedef double (*pt2calculateA)(double);
pt2calculateA calculateA[2];
public:
myClass () {
calculateA[0] = &calculateA1; //->error
calculateA[1] = &calculateA2; //->error
}
double calculateA1(const double& arg) const {
...
}
double calculateA2(const double& arg) const {
...
}
}
Run Code Online (Sandbox Code Playgroud) pointers ×10
c ×6
c++ ×3
function ×2
arrays ×1
class ×1
coldfusion ×1
components ×1
debugging ×1
dereference ×1
destructor ×1
iso ×1
malloc ×1
printf ×1
standards ×1
string ×1