0

It's been a while since I have written any C++, so when looking up some basic examples to get me started, I was surprised to see something like this:

#include <iostream>

class TestClass {
public:
    void testMethod(){
        std::cout << "Hello!";
    }    
};

int main()
{
  TestClass test;     // Not being instantiated
  test.testMethod();  // Method still able to be called successfully!
}

How is it possible that a non-static method of a class can be called without an instance of the class being created first?

Working example: http://cpp.sh/3wdhg

FoxMulder900
  • 1,272
  • 13
  • 27
  • 8
    Change comment "// Not being instantiated" to "// Instance created" – QuentinUK Jul 04 '19 at 23:46
  • So simply declaring the variable with a type creates an instance? – FoxMulder900 Jul 04 '19 at 23:47
  • 1
    There it does. Add a constructor and get that to put out a message. "resource acquisition is initialization" – QuentinUK Jul 04 '19 at 23:48
  • I've seen several cases (including my first IRL C++ course!) where the instructor used the word "instantiation" when they actually meant "initialization". Maybe that is the source of the confusion here. – M.M Jul 05 '19 at 00:11
  • 1
    "instantiation" means to create an instance of the object, "initialization" refers to the setting of initial values for the object – M.M Jul 05 '19 at 00:13
  • 1
    *Not a dumb question for a beginner. –  Jul 05 '19 at 03:53

2 Answers2

2

TestClass test; is syntax for declaring a variable of type TestClass. The variable is an instance of the type. In this case it is an instance of TestClass.

Why is a non-static class method able to be called ...

Because you've created an instance.

... without an instance of the class being created first?

Your premise is false.

eerorika
  • 232,697
  • 12
  • 197
  • 326
1

The thing is, it actually is being instantiated. test is an instance of TestClass. Thus, test.testMethod() is calling a non-static method on an instance of the class.

  • Very interesting, not what I expected. Is there a name for this behavior? – FoxMulder900 Jul 04 '19 at 23:54
  • What did you expect? I don't understand why it wouldn't be an instance of the class. –  Jul 04 '19 at 23:56
  • I expected to be required to call the constructor directly: `TestClass test = TestClass();` – FoxMulder900 Jul 05 '19 at 00:01
  • 3
    You're confusing C++ with Java. In Java, `test` is not an instance of `TestClass` but rather a *reference* to an instance of `TestClass`. But in C++, `test` is an instance directly. C++ has different syntax for references. That's just how the languages work. – Raymond Chen Jul 05 '19 at 00:15
  • @FoxMulder900 you can never "call the constructor" in C++. Instead, you write code which specifies creation of an object, and part of the process of the object creation involves a constructor call (sometimes). `TestClass test;` specifies creation of an object . So does `TestClass test = TestClass();`, the two codes are similar but differ in how the object is initialized (e.g. the latter would set member integer variables to `0` if you had them, and the former won't) – M.M Jul 05 '19 at 00:16
  • Thanks RaymondChen and M.M I understand now! I was indeed thinking about things through a Java lens. I'm remembering now that objects in C++ cannot be null, only references can. I would word this question better if I was to ask it again, I was unaware that default constructors would be called automatically upon declaring the variable. – FoxMulder900 Jul 05 '19 at 00:24
  • @Fox [The truth is out there.](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) I strongly recommend you take advantage of the link and pick out a good book. C++ is very different from other modern languages. Coming in from C and Java the way I did I figured I knew enough to just pick it up. I was wrong. I rewrote a LOT of code after investing in a few good books. Even after you have unlearned all of the C and Java-isms, C++ is probably the most complicated language in wide use today and online tutorials are grossly insufficient. You need quality references. – user4581301 Jul 05 '19 at 00:58
  • Side note: Since you're coming in from Java, the next major minds will likely be [RAII](https://stackoverflow.com/questions/2321511/what-is-meant-by-resource-acquisition-is-initialization-raii) and [The Rules of Three ,Five, and Zero](https://en.cppreference.com/w/cpp/language/rule_of_three). – user4581301 Jul 05 '19 at 01:00
  • 1
    @Fox got you. Yeah, in C++, merely declaring an object of that type creates an instance of the object. –  Jul 05 '19 at 03:52