Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

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

MyClass obj = null;
obj.Function(); 


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?
Posted
Updated 22-Nov-11 16:55pm
v5
Comments
Stefan_Lang 22-Nov-11 8:28am    
Your question is tagged as C++, C#, and Java. But what is it? Don't tag a question with values that don't apply! As pointed out in my solution, this is not valid C++ code.

Besides, do you actually get an error and are aasking us to resolve it, or is that a hypothetical question to help you understand a concept? Your question is rather vague about that.

In any case, what this code is doing, speaking figuratively, is take a bucket of water with some fish in it, then empty it, and then try to fish in it. Since there is neither water nor fish, how hard is it to understand that this won't work?
Sergey Alexandrovich Kryukov 17-Apr-13 17:22pm    
Please disregard my previous message I posted on this page (deleted). I meant to address it to some other member.
Sorry for the inconvenience.
—SA

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.
C++
MyClass obj=new MyClass

Now the Other thing is that in your class you are using
C++
void Function() { }
which is interpreted as
C++
void
as a Return Type and
C++
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.
 
Share this answer
 
v2
Comments
Stefan_Lang 22-Nov-11 8:21am    
Your first example line of code should be:
MyClass *obj = new MyClass;

function is actually not a keyword in C++, neither lower nor uppercase. I have to admit I had to look that up, wasn't sure myself :-) I do agree though that 'function' is not a proper name for a function or anything else in C++!

On a sidenote, neither new nor void are uppercase! Both are lowercase, and C++ is case sensitive

P.S.: all of this is assuming C++, as you mentioned C++ specifically. If you had another language in mind, then maybe you should just fix the language you specified there ;-)
Manoj K Bhoir 23-Nov-11 6:41am    
Thanks! :)
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.
 
Share this answer
 
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.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 12-Jul-13 10:00am    
Exactly, a 5.
I would add that in .NET, apparently, you can call a static method not creating any instance, as the call can be done using the syntax TypeName.MethodName(...). Actually, this is more the matter of syntax and conventions than the mechanisms. In all cases this is all about having the implicit "this" method parameter or not.
—SA
You have'nt created the Object.
MyClass obj=new MyClass();

The 'new' keyword will allocate memory..
 
Share this answer
 
v2
Comments
Silju MC 22-Nov-11 6:35am    
I know the object is created with new keyword. But my doubt is if we doesn't create the object using 'new' how the exception is coming? what are the things happening behind?
To call a nonstatic function of a class, we need to create an object of that class by allocating memory. Because nonstatic function can only be called by instance of class. Instance cannot be null to call a method.
 
Share this answer
 
in the code you pasted here, you are making the declaration of an object of class but initialization is missing.
if you use any object without initialization, you will get object reference exception.
for your case you need to initialize your class object by calling a contructor of your class with New keyword.
i.e
C#
MyClass obj = new MyClass()

here MyClass() is the 0 argument constructor of your class, you can find it inside your code behind file or designer.
 
Share this answer
 
First of all you are not creating New Object of the Class.If you want to access any function or Method or a variable of particulate class, otherwise create the class as static to call the function like class_name.function_name, r u from comp india
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900