0

I'm a little new to C++, mostly deal with some embedded C development, but I don't do much with classes. I saw this in a Qt example header file:

class QUdpSocket;

Is this a class constructor? I know QUdpSocket is a class of it's own, in a header file, and part of the QAbstractSocket Class, but I've never encountered it this way. It confounds me a bit, can someone shed a little light on this? As I said, new to C++ and learning...

Thanks :-)

Jedi Engineer
  • 493
  • 3
  • 11
  • 29
  • if you know C, then you must have heard of forward declaration – BЈовић Mar 06 '14 at 13:34
  • 1
    It is not a constructor. It is a forward declaration of the class QUdpSocket. http://stackoverflow.com/a/4757718/2296458 – Cory Kramer Mar 06 '14 at 13:34
  • Sorry guys, I've really never used those in C. Maybe simple structures, but never classes. Most of what I wrote didn't require classes. Thanks for the insight! – Jedi Engineer Mar 07 '14 at 14:36

5 Answers5

2

It's proper name is incomplete (forward) declaration. Incomplete declaration does not define any class members. You can't declare any object or refers to members of this class until the class is fully defined.

However you can make pointers to this struct, but only if they don't need size of the class.

Inclomplete declaration is used to suggest compiler that this class will be defined somehere later and we want use it now in other class/function. This declaration is for purpose if we have two class and one of them use object of another class and vice versa.

struct first;

struct second{
   first* f;
}

struct first{
   second* s;
}
Ardel
  • 315
  • 2
  • 9
  • Thanks! The reason (I stated above) that I posted this question is because this is my first time working with classes at this level. Some of my embedded functions had them in the development libraries, but what made me ask was when I coded this: `udpSocket = new QUdpSocket(this);` and the compiler told me I couldn't use the "this" pointer to a class member, despite having included the class header and constructing everything beforehand. Adding `class UdpSocket;` to the beginning of my header fixed that. And my code runs nicely too. – Jedi Engineer Mar 07 '14 at 14:43
1

That is not a constructor -- it's a forward declaration. I am guessing this appears somewhere at the top of a header file. Programmers often do this to make the compiler aware of this class's existence early on (to avoid errors because a class name is not recognised), before declaring the class's actual content later on.

Lee White
  • 3,649
  • 8
  • 37
  • 62
  • I had included a header file which had the class in it. I had the line: `udpSocket = new QUdpSocket(this);` in my code, and despite including the header with the class, it told me that I couldn't use "this" - that changed when I put `class QUdpSocket;` in the beginning of my header file. Thanks again!! – Jedi Engineer Mar 07 '14 at 14:39
1

This is forward declaration. Constructor is nothing but a method. The forward declaration in C++ allows us to declare methods and classes without specifying their actual implementation at a given point of time, leaving that for later if necessary. This can improve the maintainability of the code and help us solve problems related to cyclic dependencies and performance, in certain situations.

Go through this: Forward declaration

And read about C++ classes. Because this is quite basic concept for a C++ development.

Amit Shakya
  • 1,396
  • 12
  • 27
  • Thanks very much for the info! That was helpful. As mentioned, this is my first time around using classes... catching up slow. This really helped me understand so much more... Thanks!! – Jedi Engineer Mar 07 '14 at 14:37
0

This is forward declaration, google about it and you will know.

Smitt
  • 178
  • 7
0

Thank you all for the information. I have used forward declarations, it turns out, with functions in a program. I never knew what they were called. Thanks for all the answers, Thanks for the info, Cheers to all!

Jedi Engineer
  • 493
  • 3
  • 11
  • 29