Coding in header vs separate cpp

I found that some code won't work unless declared on the header file itself. For instance, with the following code, "Hello World" will be printed when test() is called:

//Myclass.h
class Myclass {
   private:
        SoftwareSerial *ss;
   public:
        void test() {
            ss = & SoftwareSerial(0,1);
            ss->begin(9600);
            ss->print("Hello World");;
        };
};

But if I just declare the method test() on the header, but code it on a separate cpp as usual with the exact same code, it compiles but doesn't output anything.

//Myclass.cpp
void Myclass::test() 
{
        ss = & SoftwareSerial(0,1);
        ss->begin(9600);
        ss->print("Hello World");
};

Why is that? Am I doing something wrong? Any workaround?

You must create a constructor that gets an instance of SoftSerial passed as an argument.

For example have a look at the cozir lib - Arduino Forum -
zip file is attached at bottom of the first post

        ss = & SoftwareSerial(0,1);

Why are you doing software serial on the hardware serial port?

You must create a constructor that gets an instance of SoftSerial passed as an argument.

Why? I mean, why can't I create a new instance INSIDE my new class? (I'm a Java developer, if that helps). More specifically, why can I create it in the header file BUT NOT in the .cpp??
I'm very interested because, even though in this case I found a workaround (as I said, by coding directly into the header file) I have no clue why it works in one way and not the other, so I'm afraid I'll walk into the same problem again.

Why are you doing software serial on the hardware serial port?

Because the hardware serial port in the Arduino UNO comes with a serial-to-usb converter so I can debug its output from my computer using the serial monitor that comes with the arduino IDE. Of course, once it works I'm planning on using it on a different port.

The code you posted, whether in the header file or the source file, makes little sense.

Why are you creating a new instance of software serial every time the function gets called? It would make sense to create ONE instance, when the class gets instantiated, not every time the method gets called.

You still haven't explained why you are doing software serial on the hardware serial pins.

Why are you creating a new instance of software serial every time the function gets called

It's a test (you know, the funcion's name is "test"). I created the test only to explore why the initialization of SoftwareSerial worked when declared on the header but not on the .cpp. That's why I don't even bother to delete that instance.

You still haven't explained why you are doing software serial on the hardware serial pins.

Yes I did:

Because the hardware serial port in the Arduino UNO comes with a serial-to-usb converter so I can debug its output from my computer using the serial monitor that comes with the arduino IDE. Of course, once it works I'm planning on using it on a different port.

The easiest way to debug the output of SoftwareSerial is of course to read it directly through the serial monitor, and that's what I'm doing. The pins have hardware serial capabilities, but that doesn't mean that you HAVE to use that capabilities.

BTW the correct answer was provided to me by the cool guys on the C++ IRC channel. I leave it here just in case someone else needs the answer.

ss = & SoftwareSerial(0,1);

Here I'm pointing ss to a temporary. Trying to dereference it later is illegal and results in unexpected behaviour. It shouldn't even work.

So just use the "new" keyword to create a new instance and there you go

ss = new SoftwareSerial(0,1);