What this code is doing
-
I want to know what is happening in these lines of code: a)
public class cAppBarDisplay:AppBar
{
cMessageProcessing CMS;
//some code here
public cAppBarDisplay(cMessageProcessing cMP)
{
CMS = cMP;//exactly what is happening here
//cMessageProcessing is another class,
}}
->cMessageProcessing CMS; By this mean are we creating a refrence? ->and what the constructer is actually doing? CMS & cMP both are refrence of same class:confused: Thanks in advance, plz describe in a bit detail.
-
I want to know what is happening in these lines of code: a)
public class cAppBarDisplay:AppBar
{
cMessageProcessing CMS;
//some code here
public cAppBarDisplay(cMessageProcessing cMP)
{
CMS = cMP;//exactly what is happening here
//cMessageProcessing is another class,
}}
->cMessageProcessing CMS; By this mean are we creating a refrence? ->and what the constructer is actually doing? CMS & cMP both are refrence of same class:confused: Thanks in advance, plz describe in a bit detail.
humdumof wrote:
->cMessageProcessing CMS; By this mean are we creating a refrence?
You created a field called CMS, which you can set to reference an instance of cMessageProcessing. This happens when you do CMS = cMP for example.
humdumof wrote:
CMS = cMP;//exactly what is happening here
You assign the parameter cMP to the field CMS. They now hold the same reference to an instance of the cMessageProcessing class (not a reference to the class itself).
-
I want to know what is happening in these lines of code: a)
public class cAppBarDisplay:AppBar
{
cMessageProcessing CMS;
//some code here
public cAppBarDisplay(cMessageProcessing cMP)
{
CMS = cMP;//exactly what is happening here
//cMessageProcessing is another class,
}}
->cMessageProcessing CMS; By this mean are we creating a refrence? ->and what the constructer is actually doing? CMS & cMP both are refrence of same class:confused: Thanks in advance, plz describe in a bit detail.
-
humdumof wrote:
->cMessageProcessing CMS; By this mean are we creating a refrence?
You created a field called CMS, which you can set to reference an instance of cMessageProcessing. This happens when you do CMS = cMP for example.
humdumof wrote:
CMS = cMP;//exactly what is happening here
You assign the parameter cMP to the field CMS. They now hold the same reference to an instance of the cMessageProcessing class (not a reference to the class itself).
AlwiNus wrote:
You assign the parameter cMP to the field CMS. They now hold the same reference to an instance of the cMessageProcessing class (not a reference to the class itself).
Not a refrence to the class itself? By this what u mean?:confused: also CMS=cMP; Does it mean we can access all the methods of cMessageProcessing thru CMS? & what is use of doing all this in constructor?
-
AlwiNus wrote:
You assign the parameter cMP to the field CMS. They now hold the same reference to an instance of the cMessageProcessing class (not a reference to the class itself).
Not a refrence to the class itself? By this what u mean?:confused: also CMS=cMP; Does it mean we can access all the methods of cMessageProcessing thru CMS? & what is use of doing all this in constructor?
humdumof wrote:
Not a refrence to the class itself? By this what u mean?
When you use a class you create an instance of it. Kinda like if i want a bowl of cereal, i have to get myself a bowl and some cereal, and probably some milk. Then my bowl of cereal that i can eat, would be an instance of a bowl of cereal... if you follow me. When you pass the instance of your class to the constructor your passing a reference (pointer) to it.
humdumof wrote:
also CMS=cMP; Does it mean we can access all the methods of cMessageProcessing thru CMS? & what is use of doing all this in constructor?
Yes, you can access all the methods of the class through CMS. The use of this, is that you can make use of the same instance of a class, in as many different classes as you like. So you can make 1 instance of your class, and pass it to 2 other classes for example. Then both classes will work on the same instance. EDIT: As for why you should actually do this in the constructor: If the new class depends on having an instance of cMessageProcessing (ie it uses it) and it does not get passed a reference or create its own instance, then it will probably crash. By passing it to the constructor, you can guarantee that the new class will always have an instance of cMessageProcessing, it would be impossible to instatiate the class (ie myClass = new MyClass()) without passing the correct parameters.
My current favourite word is: Bauble!
-SK Genius