-1

I've got a namespace, with a class inside. As shown below;

class testClass {

public:
    testClass() { std::cout << "neat" << std::endl; };
    ~testClass() { };
    void print() {
        std::cout << "printOne" << std::endl;
    }

};

namespace test {
    class testClass;

    class testClassTwo {
    public:
        void printTwo() {
            std::cout << "printTwo" << std::endl;
        }
    };
}

I know that I can inherit from the testClass using the normal way of

class testClassTwo : public testClass

But I've seen something along the line of what's within the namespace

class testClass;

I haven't been able to find a solid explanation of what this actually does, i'm assuming inheriting something from the testClass class.

I understand the simple stuff about namespaces such as;

test::testClassTwo classobj;
classobj.printTwo();

I can also compile; test::testClass;

but can't actually do anything with it.

Would anyone be able to forward me to some reading material or a quick explanation of what's actually going on when I do this?

Dannys
  • 335
  • 1
  • 5
  • 12

3 Answers3

1

I haven't been able to find a solid explanation of what this actually does, I'm assuming inheriting something from the testClass class.

That's an incorrect assumption. class testClass is a forward declaration of testClass data type. It lets you declare pointers and references to testClass objects without importing the corresponding header.

More information on forward declarations can be found in this Q&A.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

It depends on the context. Where is it in the code?

In the code you pasted, it declares (tells the compiler) that a class called testClass exists in the test namespace. (Read more: forward declaration)

It should compile fine, but linking would throw an undefined rederence error. There is no definition for test::testClass.

Community
  • 1
  • 1
Ivan Rubinson
  • 3,001
  • 4
  • 19
  • 48
  • This is just a simple bit of code that I'm using to test things out, but seems like I was looking for forward declaration. – Dannys Jul 18 '16 at 11:09
0
class testClass;

This is nothing more than a forward declaration. Usually used to prevent circular dependencies.

Community
  • 1
  • 1
Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122