类或结构的实例是否等于c ++中指针的实例? [英] Are the instance of classes or structures equal to pointer of those in c++ ?

查看:107
本文介绍了类或结构的实例是否等于c ++中指针的实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




此代码中的两个(a)是否相等?



A级();

A a;







A * a;



还有关于结构

解决方案

没有。

当你声明指向某个东西时无论它是什么,你都声明一个变量,其类型是指向对象的指针 - 而不是对象本身的实例。最初,指针将为null - 它指向没有实例,因此任何尝试使用它而不为其分配实例地址将导致错误。在一个应用程序中,所有指针都具有相同的长度(尽管这可能因系统而异:x86指针是32位或4字节长,而a,x64指针将是64位或8字节长。)



当你声明一个类或结构的实例时,你要在堆栈上为整个类/结构分配空间,而不仅仅是指针。


出于问题的目的(并忽略语法问题),使用''class''或''struct''会产生相同的行为(这里有其他差异,但不在你的问题范围内) 。



创建2个对象时:



 A a1 ; 
A * a2 = new A();





然后在堆栈上创建a1,并在堆上创建a2。施工方式不同。毁灭发生的方式不同。方法和数据成员的访问方式不同。



 {
A a1; // 在堆栈上创建a1
A * a2 = A(); // 在堆上创建a2
a1.foo();
a2-> foo();
a1。 value = 42 ;
a2-> value = 42 ;
delete a2; // ~a2()调用
} // 〜范围退出时调用的a1()


除了以前答案。



 A a;  //  在堆栈中创建的对象 
A * pa =& a; // 但它仍然有一个指针,你可以在下一步获得它
// 现在,如果你调用那两个实例的任何方法,你就可以在同一个对象上调用它。
a 。方法();
pa-> Method();
// 并且在同一对象上也执行了set属性
a。 value = 42 ;
// pa->值现在也是42,因为pa指向同一个对象



作为 a 的例子,在范围内删除了它,你创造了你不能使用 pa 该范围之外的变量(在解决方案2中你可以看到)

另外你不应该通过调用 delete

但你可以比较指针作为一个例子:

 A a1;  //  初始化A类的2个实例 
A a2;
A * pa = bConditionton? & a1:& a2; // 根据bCondition变量init pa
// .....
if (pa ==& a1)
{
// 语句,如果pa指向a1对象
}
其他
{
// 语句,如果pa指向a2对象
}



问候,

Maxim。


Hi
Are two (a) in this codes equal?

class A ();
A a;

OR

A* a;

And also it about structures

解决方案

No.
When you declare a pointer to something, whatever it is, you are declaring a variable whose type is "pointer to object" - not an instance of the object itself. Initially, a pointer will be null - it points to no instance so any attempt to use it without assigning an instance address to it will cause an error. Within an application, all pointers will have the same length (though this can differ from system to system: a x86 pointer is 32bit or 4 bytes long, while a, x64 pointer will be 64 bits or 8 bytes long.)

When you declare a instance of a class or structure you are allocating space on the stack for the whole class / structure rather than just enough for a pointer.


For the purpose of your question (and ignoring the syntax problems), use of either ''class'' or ''struct'' generates identical behavior (there are other differences here but not in the scope of your question).

When you create 2 objects with:

A a1;
A* a2 = new A();



then a1 is created on the stack, and a2 is created on the heap. Construction occurs differently. Destruction occurs differently. Methods and data members are accessed differently.

{
    A a1;  // create a1 on stack
    A* a2 = new A(); // create a2 on heap
    a1.foo();
    a2->foo();
    a1.value = 42;
    a2->value = 42;
    delete a2;  // ~a2() called
}  // ~a1() called when scope exits


In additionally to previous answers.

A a; // object created in stack
A * pa = &a; // But it still have a pointer and you can get it next way
// Now if you call any method of the those 2 instances you call it on same object.
a.Method();
pa->Method();
// And set property performed also on same object
a.value = 42;
// pa->value right now is also 42 as pa pointed to same object


As instance of a deleted in scope there it was created you can''t use pa variable outside of that scope (in solution 2 you can see that)
Additionally you should not destruct pa object by calling delete
But you can compare the pointers as an example:

A a1; // Initialize 2 instances of the A class
A a2;
A * pa = bConditionton ? &a1 : &a2; // according to bCondition variable init pa
//.....
if (pa == &a1) 
{
// statements if pa pointed to a1 object
}
else
{
// statements if pa pointed to a2 object
}


Regards,
Maxim.


这篇关于类或结构的实例是否等于c ++中指针的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆