0

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?

user74200
  • 263
  • 4
  • 10

2 Answers2

4

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);
3

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!

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Thanks. The class calls some dodgy fortran routines, I can not really elaborate without showing you the whole framework, the information I give you would be always incomplete. I am not really a C++ programmer, I am a physicist, who uses a C++ framework to analyze some data. But I want to understand it. Also, I have a strong feeling that the framework was not written by a programmer, but also by a physicist. – user74200 Mar 08 '18 at 21:51
  • @user74200: I suspect your feeling is accurate. – Lightness Races in Orbit Mar 08 '18 at 21:51