0

I have a Test class like following

#include "Test.h"
#include <iostream>
void Test::Display()
{
    std::cout << "Hello"<<std::endl;
}

I don't understand how can I call a function in Test class without creating an object of Test class

void TTest(Test* ptrTest);
int main()
{
    //Test* t = nullptr;// = new Test();
    TTest(NULL);
    while (true)    {   }
    std::cout << "Hello World!\n";
}

void TTest(Test* ptrTest)
{
    if (ptrTest == NULL)
    {
        std::cout << "Obj is Null" << endl;
    }
    ptrTest->Display();
}

Output is: Obj is Null Hello

Help me in understanding this code, please.

cpp_pravo
  • 11
  • 2
  • 2
    I'm not clear on what you're asking. A member function is *part* of a class, so you have to have an object to call it. You can only call global, or static member functions without an object. – cigien Apr 23 '20 at 13:51
  • 4
    `ptrTest->Display();` invokes *undefined behavior* if `ptrTest` is a null pointer – UnholySheep Apr 23 '20 at 13:51
  • 1
    @cigien look at the code in main, I am nowhere creating an object, but I am getting the output from Display() function defined in Test class... – cpp_pravo Apr 23 '20 at 13:53
  • 1
    @UnholySheep Hoe am I able to see the output Hello which is coming from Display() function – cpp_pravo Apr 23 '20 at 13:55
  • @cpp_pravo Your code has *undefined behaviour*, think about what that really means. Also think about what you expected to happen instead. Because whatever that was, it was wrong. – john Apr 23 '20 at 13:56
  • @john I would expect an exception – cpp_pravo Apr 23 '20 at 14:01
  • 2
    @cpp_pravo You've been told (several times) that your code has undefined behaviour. This is another way of saying that C++ puts no limitations on what your program should do. In other words anything could happen and the rules of C++ would not be broken. For some reason beginners struggle with this concept. – john Apr 23 '20 at 14:01
  • 1
    @cpp_pravo That expectation is wrong. C++ does not say that calling a function without an object should cause an exception. That might be true in some languages (Java for instance) but it's not true in C++. All C++ says is that your program has (wait for it) undefined behaviour. – john Apr 23 '20 at 14:02
  • in case you know what it means to multiply both sides of an equation by zero in maths, undefined behavior is similar, anything goes, but the significance is zero. You need to fix your code, because really it does not "work" – 463035818_is_not_an_ai Apr 23 '20 at 14:02
  • @john hmm, it looks like C++ is behaving like C in this case, C# gives NullRefrenceException – cpp_pravo Apr 23 '20 at 14:06
  • 1
    c++ doesnt put "make it impossible to make mistakes" very high on its list, rather "don't pay for what you don't need". Checking pointer for not being NULL on each and every pointer access is a cost that many don't want to pay – 463035818_is_not_an_ai Apr 23 '20 at 14:08
  • @cpp_pravo I'm sure that's correct, C# is more like Java than C++. – john Apr 23 '20 at 14:08
  • In a very real sense, you *don't have* a C++ program. You have some text that you tricked a C++ compiler into turning into an executable. – Caleth Apr 23 '20 at 14:11
  • diffferent lanuages are different, personally I definitely prefer a "you have to know what you are doing" over a "we assume the most stupid user and leave anything that could be used wrong out" – 463035818_is_not_an_ai Apr 23 '20 at 14:11
  • 1
    @john I think I got the point, my program has undefined behavior. – cpp_pravo Apr 23 '20 at 14:16
  • C++ is not a nanny language. It gives you enough rope to shoot yourself in the foot. You break the rules by doing undefined behavior, the compiler can reward you by crashing, or worse... by appearing to work. Or by appearing to work until you release the program to the public at which point it emails everyone's browser history to their grandmothers and then formats their hard drives. – Eljay Apr 23 '20 at 14:20
  • BTW: on my machine, running the program causes `runtime error: member call on null pointer of type 'Test'` and abends. Undefined behavior can do that. – Eljay Apr 23 '20 at 14:25
  • Is the question related to this question? https://stackoverflow.com/questions/12333754/equivalent-of-java-static-methods-in-c – darclander Apr 23 '20 at 16:55

1 Answers1

4

You're not.

Your program has undefined behaviour.

It just doesn't "crash" or error because it doesn't have to (and because you have no data member access to maybe, possibly trigger a memory access violation).

It can "appear" to work because member functions aren't stored "in" class objects; they're still in existence in your compiled program. On typical implementations, it'll be invoked by your computer with nullptr as its hidden, implicit first "this" argument. So if you don't "use" that pointer you may not witness any symptoms.

But even that assumes the compiler hasn't used its ability to assume you have not written a program with UB to do some other crazy sh!t that causes mayhem and havoc beforehand.

Don't do this.

Asteroids With Wings
  • 17,071
  • 2
  • 21
  • 35