I'm coming (very recently) from C#, where I am used to instantiating objects like this:
Phone myPhone = new Phone();
simply writing
Phone myPhone;
essentially creates a holder for a class, but it is yet to be initialised so to speak.
now I'm writing a small class in C++ and I have a problem. Here is the pseudo code:
Phone myPhone;
void Initialise()
{
myPhone = new Phone();
}
void DoStuff()
{
myPhone.RingaDingDong();
{
In fact this is a little misleading as the above code is what I would LIKE to have, because I want to be able to put all my initialisation code for lots of things into one neat place. My problem is that the line inside initialise is unnecessary in C++, because before that, a new instance is already created and initialised by the very first line. On the other hand if I put the just the first line inside Initialise() I can no longer access it in DoStuff. It is out of scope, (not to mention the differences between using 'new' or not in C++). How can you create simply a holder for a class variable so I can initialise it in one place, and access it in another? Or am I getting something fundamentally wrong?
Thanks in advance!