关于对象创建的小疑问 [英] Small doubt about object creation

查看:51
本文介绍了关于对象创建的小疑问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

Hi All,

class MyClass
{
	int x;
	
	void Function()
	{
	}
}

MyClass obj = null;
obj.Function(); 



在此代码段中,异常如何出现(obj.Function();)?后面发生了什么事?我认为该功能已经在内存中.它怎么没有得到参考?创建对象时会发生什么?我知道当我们创建一个对象时,只有在堆中分配了足够内存的成员.


在此代码片段中,我实际上是说,如果我们将一个对象分配为null然后访问一个函数,那后面会发生什么?即,



In this code snippet how the exception is coming (obj.Function();)? What is happening behind? I think the function is already in the memory. How it doesn''t get the reference? What is happening when we create an object? I know when we create an object only the members allocated with sufficient memory in heap.

Edit :
In this code snippet I actually mean what is happening behind if we assign an object to null and then accessing a function? ie,

MyClass obj = new MyClass();
obj.Function(); 
obj.x;


这里的函数调用是如何发生的?我认为函数和成员变量位于单独的内存(堆栈和堆)中.


Here how the function call is happening? I think functions and member variables are in separate memory(stack and heap). so how object can access both function and members?

推荐答案

首先,您不创建类的新对象.如果要访问任何函数或方法, perticuler类首先必须使用New Keyword创建该对象(如果该类).
First of all you are not creating New Object of the Class.If you want to access any function or Method of perticuler class first you have to create the Object if that Class using New Keyword.
MyClass obj=new MyClass


现在,另一件事是,在您的班级中,您正在使用


Now the Other thing is that in your class you are using

void Function() { }

,它被解释为

void

作为返回类型,而

Function

作为方法名称. C ++功能"很特别
您不能将其用作Delcare方法的名称关键字.这就是为什么您得到它的原因
我认为,如果您尝试使用其他名称插入"Function",它将解决您的问题.

as a Method Name.But in C++ "Function" is the Special
Keyword you can''t use it as Name to Delcare Method.Thats why you are getting
this exception.I think if you try other name insted of "Function" it will solve your problem.


此代码不是C ++:

1.关键字class在C ++中为小写

2.关键字null在C ++中不存在

3.您不能为对象分配简单的值,指针或null代表的任何其他术语,除非您指定适当的赋值运算符或构造函数,并接受该类型的参数.你都没做.

在C ++中执行此类操作的唯一方法是定义一个指向该类的指针并将其分配为0,然后尝试取消对其的引用以调用函数.这根本不会调用该类(或函数),因为在尝试取消引用0时会崩溃.
This code is not C++:

1. the keyword class is lowercase in C++

2. The keyword null doesn''t exist in C++

3. You cannot assign a simple value, pointer, or whatever else the term null represents, to an object, unless you specify an appropriate assignment operator or constructor taking an argument of that type. You did neither.

The only way to do something like this in C++ is by defining a pointer to that class and assign it to 0, then try to dereference it to call a function. This will not invoke the class (or function) at all, since it will crash upon trying to dereference 0.


至少在C#中(但我认为它在Java中同样有效),当您尝试访问非静态成员,即使它是一个方法,也要在调用该方法之前进行验证.

在C ++中,您可以创建一个空的实例方法,将任何指针(甚至非null)声明为您的类类型,然后调用该方法.实际上,"this"指针将为null或损坏,但是您甚至可以在方法本身内部使用它(您可以启动该方法,就像(this == NULL)return;一样.).

但是对于C#,这并不意味着它没有找到该方法.这意味着它是对实例的无效访问,因为它为null.

毕竟,如果您的方法不需要访问"this"实例,那么它应该是静态的.
At least in C# (but I think it works equally in Java), when you try to access a non-static member, even if it is a method, a validation is done before the method is called.

In C++, you can create an empty instance method, declare any pointer (even non-null) as your class type and call the method. In fact, the "this" pointer will be null or corrupted, but you can even use that inside the method itself (you can start the method as if (this == NULL) return;).

But for C#, this does not mean it is not finding the method. It means it is an invalid access to an instance, as it is null.

After all, if your method does not need to access the "this" instance, then it should be static.


这篇关于关于对象创建的小疑问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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