Forward declarations of class member enums?

S

Steven T. Hatton

This is not a big deal. It just bothers me that I don't see a way around
including the header for QVariant in the following:

#ifndef _XML_IMPL_INTERNAL_H_
#define _XML_IMPL_INTERNAL_H_

class QString;
class QVariant;

namespace xml_impl {
void unknownType(const QString& functionName, const QVariant::Type& type);
void ni(const QString& functionName);
}
#endif
/*****************************
* compiler error messages
.../../include/xml_impl/xml_impl_internal.h:7: error: expected unqualified-id
before ?&? token
.../../include/xml_impl/xml_impl_internal.h:7: error: expected ?,? or ?...?
before ?&? token
.../../include/xml_impl/xml_impl_internal.h:7: error: ISO C++ forbids
declaration of ?parameter? with no type
*****************************/

Is there any way to get around providing the definition of QVariant::Type
prior to the function declaration?
 
A

Alf P. Steinbach

* Steven T. Hatton:
This is not a big deal. It just bothers me that I don't see a way around
including the header for QVariant in the following:

#ifndef _XML_IMPL_INTERNAL_H_
#define _XML_IMPL_INTERNAL_H_

class QString;
class QVariant;

namespace xml_impl {
void unknownType(const QString& functionName, const QVariant::Type& type);
void ni(const QString& functionName);
}
#endif
/*****************************
* compiler error messages
../../include/xml_impl/xml_impl_internal.h:7: error: expected unqualified-id
before ?&? token
../../include/xml_impl/xml_impl_internal.h:7: error: expected ?,? or ?...?
before ?&? token
../../include/xml_impl/xml_impl_internal.h:7: error: ISO C++ forbids
declaration of ?parameter? with no type
*****************************/

Is there any way to get around providing the definition of QVariant::Type
prior to the function declaration?

Assuming it's your own thing, just do

class QVariantType;

and in your function (which smells of non-OO)

void unknownType(
QString const& functionName, QVariantType const& type
);

and wherever you're definining class QVariant

class QVariantType { ... };
class QVariant
{
public:
typedef QVariantType Type;
};
 
S

Steven T. Hatton

Alf said:
* Steven T. Hatton:
Assuming it's your own thing, just do
Sorry, after I sent the message I realized I had not been very clear about
the situation. QVariant is a class in Qt. It's kind of like an OO
extension of a union that uses a type property to communicate it's content
type. The variable of type QVariant::Type I was trying to pass is
actualized by one of the enumerators.

Here's the doc on QVariant if you happen to be interested:
http://doc.trolltech.com/4.2/qvariant.html
class QVariantType;

and in your function (which smells of non-OO)

You are quite perceptive. I have a stateless translator function which has
no legitimate reason to be in a class. I put it and it's cousins in a
small namespace, and exposed it through an interface namespace.

http://bridgewater.wordpress.com/2006/10/03/c-is-a-multi-paradigm-language-item-1/
void unknownType(
QString const& functionName, QVariantType const& type
);

and wherever you're definining class QVariant

class QVariantType { ... };
class QVariant
{
public:
typedef QVariantType Type;
};

Something crossed my mind today, but I'm not sure I should really admit I
entertained the notion. Following Peano one might construct enumerated
types as

struct Integer{};
struct Zero: public Integer{};
struct One: public Zero{};
struct Two: public One{};
....
struct NPlusOne: public N{};

That, in itself weighs nothing at runtime because there are no virtual
functions. OTOH, the ordering is lost at runtime. It has the advantage
over enumerations that you cannot use a value you have not explicitly
specified. One could do something similar to what Robe did with r++
http://websvn.kde.org/branches/work...uages/cpp/parser/ast.h?rev=590725&view=markup

And for grins you could even throw in a static const char* so you could dump
the name if you needed it for something like an XML builder.

Yet another option might be to use template metaprogramming to recursively
enumerate the items. Yet another would be to do some kind of
initialization at load time assigning the value of an incremented counter
to each instance sequentially. Why don't I like enums? Mostly because the
aren't really enumerations.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top