way of defining class in a namespace

前端 未结 6 759
悲哀的现实
悲哀的现实 2020-12-11 08:11

I defined a class in a namespace in a header as follows

#ifndef _c1_
#define _c1_

namespace classspace
{
    class Aclass;
}

class Aclass
{
    //body
};

         


        
相关标签:
6条回答
  • 2020-12-11 08:41
    namespace classspace
    {
        class Aclass;
    }
    

    This declares a class inside a namespace.

    class Aclass
    {
        //body
    };
    

    This declares and defines a different class with the same name in the global namespace

    class classspace::Aclass
    {
        //body
    };
    

    This defines the class you previously declared in the namespace.

    void main()
    {
        classspace::Aclass b;
    }
    

    This tries to instantiate the class declared in the namespace. If that class hasn't been defined (only declared), then it is incomplete and can't be instantiated.

    The Qt example involves two classes: Ui::MainWindow, and MainWindow in the global namespace. The one in Ui has only been declared, so is incomplete in the header. You can do various things with it, such as declare a pointer to it, but you can't instantiate it.

    Presumably, there is a separate source file which defines the Ui class, instantiates one, and sets the pointer to point at it.

    By the way, you shouldn't use reserved names for your include guards, or for anything else. Also, the return type of main must be int.

    0 讨论(0)
  • 2020-12-11 08:52

    The class definition must be in the same namespace you declared the class in.

    As for the Qt example, the MainWindow declared outside of the namespace isn't the same class.

    It uses the Pimpl idiom. The MainWindow class declared in the namespace is used as a member in the MainWindow class declared outside, and is qualified with its namespace :

    Ui::MainWindow* ui;
    

    The definition of this class is put somewhere else (in a different .cpp file) where it should be in the Ui namespace, or with definitions prefixed with the namespace.

    0 讨论(0)
  • 2020-12-11 08:53
    namespace classspace
    {
        class Aclass;
    }
    
    class Aclass
    {
        //body
    };
    

    These two classes are two different classes (with same name).

    You're using classspace::Aclass which has not defined yet. Add the body in the namespce:

    namespace classspace
    {
    
    class Aclass
    {
        //body
    };
    
    }
    
    0 讨论(0)
  • 2020-12-11 08:55

    You declare the class within the namespace but define it outside the namespace. So the declaration is:

    classspace::Aclass
    

    but the definition defines the implementation for:

    ::Aclass
    

    Namespaces should match for declaration and definition.

    0 讨论(0)
  • 2020-12-11 08:55

    Imagine

    namespace foo
    {
      class AClass;
    }
    namespace goo
    {
      class AClass;
    }
    
    class AClass
    {
    }
    
    int main()
    {
      AClass myClass;
    }
    

    Which one are we using? foo or goo?

    You must specify, either by calling all uses of AClass with the namespace name, i.e foo::AClass, or by placing all uses of it inside

    namespace foo
    {
    
    }
    
    0 讨论(0)
  • 2020-12-11 08:57

    Namespaces are essentially packages. In your Qt example, the class is forward declared. One way to use namespaces is as follows:

    namespace MyNamespace
    {
      class MyClass
      {
      };
    }
    

    If you want to do it like your first example, then you must forward declare it. For example,

    namespace MyNamespace
    {
       class MyClass;
    }
    
    class MyClass
    {
        //body
    };
    
    0 讨论(0)
提交回复
热议问题