Q:

Assigning a value to a pointer that does not point to anything in C++

I have a bit of code that looks something like this:
class MyClass
{
 void doSomething(std::string& stringvar) {
   // Does something
}}
class AnotherClass
{ /* Some data structure */ }

And then in the main, the programmer calls
MyClass* Class1;
std::string myString = "something";
AnotherClass* SecondClass; 
SecondClass = (*Class1).doSomething(myString);

My question is, what does this do, since I am defining a pointer that points to nothing and then setting its value? To what does it set a value to? A random memory slot? And where? In stack or heap? And why would someone create a class only to write a function that does something? I don't really understand the class MyClass, is it possible that doSomething actually fills some data structure? If yes, then how and where if it is also a pointer that points to nothing?

A:

As you have phrased the question, it is undefined behaviour.  That means anything is allowed to happen.  One of the things that could happen is that it works just as the programmer intended.  This is particularly likely if doSomething is a non-virtual function, and doesn't access any of the class's (non-static) member variables.
This is still undefined behaviour.  It could stop working as soon as you change something completely unrelated.
If my diagnosis is correct, the solution is to make doSomething a static member function, and invoke it as:
    secondVar = MyClass::doSomething(mystring);

A:

I am defining a pointer that points to nothing and then setting its value? To what does it set a value to? A random memory slot? And where? In stack or heap?

Well, quite. There is no answer. The results are, literally, undefined. It could send an electromagnetic pulse into my right eyeball.

And why would someone create a class only to write a function that does something?

Incompetence exists.

I don't really understand the class MyClass, is it possible that doSomething actually fills some data structure?

It's possible, but we can't tell you without seeing it.

If yes, then how and where if it is also a pointer that points to nothing?

Some people think that they can call a member function through a dodgy pointer, as long as they did not use any member variables, because "it worked for me".
Those people are wrong. The program has undefined behaviour.
I once worked with someone who habitually did this with static member functions, because they did not know that you could (should) write Class1::doSomething(myString) instead!

