Question about classes
-
Hi guys, I hope my question will be peace of cake for you. Here it is:
CmyClass cl; //CmyClass is defined elsewhere. How to define it, so I can create it in a function Init() and destroy it in function FreeAll()?
If I declare it in a function it will be created and destroyed within it. If I declare it as a global variable, it will create it in the declaration (wherever this take place in the program). Well, I can use 'CmyClass *cl', but in the code I will have to use '->' instead of '.', which ... makes me angry. Is there another approach? -
Hi guys, I hope my question will be peace of cake for you. Here it is:
CmyClass cl; //CmyClass is defined elsewhere. How to define it, so I can create it in a function Init() and destroy it in function FreeAll()?
If I declare it in a function it will be created and destroyed within it. If I declare it as a global variable, it will create it in the declaration (wherever this take place in the program). Well, I can use 'CmyClass *cl', but in the code I will have to use '->' instead of '.', which ... makes me angry. Is there another approach?Technically (AFAIK) you can't. You may however use - a wrapper of the class providing access to (something similar to lazy initialization or singleton patterns) or - use, whenever needed, a reference as alias to the dereferenced pointer to avoid pointer syntax. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Technically (AFAIK) you can't. You may however use - a wrapper of the class providing access to (something similar to lazy initialization or singleton patterns) or - use, whenever needed, a reference as alias to the dereferenced pointer to avoid pointer syntax. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
use, whenever needed, a reference as alias to the dereferenced pointer to avoid pointer syntax.
Could you give me an example? Sorry I'm new in C++ but I'm learning very fast when I see a piece of code.for instance, suppose your class having 2 methods
DoSomething
,JustDoAnotherThing
:// _pcl is the global pointer to the CmyClass object.
// myObj is just an alias to such object.
CmyClass & myObj = *_pcl;// no pointer syntax below
myObj.DoSomething();
myObj.JustDoAnotherThing()
...:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
for instance, suppose your class having 2 methods
DoSomething
,JustDoAnotherThing
:// _pcl is the global pointer to the CmyClass object.
// myObj is just an alias to such object.
CmyClass & myObj = *_pcl;// no pointer syntax below
myObj.DoSomething();
myObj.JustDoAnotherThing()
...:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
wait, friend, wait: you're going to discover some interesting issues using references. :rolleyes: :-D
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
wait, friend, wait: you're going to discover some interesting issues using references. :rolleyes: :-D
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]oh, I think I already did ... :(( I tried (global variables): CakRadioBox *prb = NULL; CakRadioBox & rb; well ... it doesn't work, it require initialization I changed it to: CakRadioBox *prb = NULL; CakRadioBox & rb = *prb; so far so good, but when in Init() I wrire: prb = new CakRadioBox(10, 134, 44, 24); rb = *prb I receive exception :rolleyes: