views:

250

answers:

6

I know the questions seems ambiguous, but I couldn't think of any other way to put it, but, Is it possible to do something like this:

#include<iostream>

class wsx;

class wsx
{
public:
wsx();
}

wsx::wsx()
{
std::cout<<"WSX";
}

?

A: 

I'm not sure what you mean. The code you pasted looks correct.

bdonlan
I was using the code as an example of what I thought it would look like; I was asking if it was possible.
Keand64
Well, it is possible, and you did it, but I'm not sure you said what you meant to say, and did what you meant to do :)
bdonlan
A: 

You did define the class. It has no data members, but that's not necessary.

SPWorley
+8  A: 

Yes, that is possible. The following just declares wsx

class wsx;

That kind of declaration is called a forward declaration, because it's needed when two classes refer to each other:

class A;
class B { A * a; };

class A { B * b; };

One of them needs to be forward declared then.

Johannes Schaub - litb
+2  A: 

In your example,

class wsx; // this is a class declaration

class wsx  // this is a class definition
{
public:
wsx();
}

So yes, by using class wsx; it is possible to declare a class without defining it. A class declaration lets you declare pointers and references to that class, but not instances of the class. The compiler needs the class definition so it knows how much memory to allocate for an instance of the class.

Greg Hewgill
+2  A: 

This is the definition of the class

class wsx
{
public:
wsx();
}

This is the definition of the constructor

wsx::wsx()
{
std::cout<<"WSX";
}

THis is a forward declaration that says the class WILL be defined somewhere

class wsx;
Shane C. Mason
+2  A: 

Yes. But it is not possible to define a class without declaring it.

Because: Every definition is also a declaration.

wmeyer