| View previous topic :: View next topic |
| Author |
Message |
unspammable@gmail.com Guest
|
Posted: Sun Jul 17, 2005 8:21 pm Post subject: forward declaration using nested typedef |
|
|
What I have is the following class hierarchy:
class Node;
class HasNodes {
Node * nodes();
};
class Node {
typedef int ValueType;
};
So far so good.
But now I need HasNodes to use the ValueType typedef, like so:
class HasNodes {
Node * nodes();
Node::ValueType* values();// Boom! "Use of undefined type Node" -- VC7
};
An obvious workaround is to move the typedef out of class Node.
But I'm somewhat partial to this style of typedefing:
class Node {
typedef int ValueType;
};
which allows me to write Node::ValueType and doesn't pollute the
namespace, as opposed to:
typedef int NodeValueType;
class Node {
NodeValueType v_;
};
Any suggestions?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
WittyGuy Guest
|
Posted: Mon Jul 18, 2005 8:19 am Post subject: Re: forward declaration using nested typedef |
|
|
[email]unspammable (AT) gmail (DOT) com[/email] wrote:
| Quote: | What I have is the following class hierarchy:
class Node;
class HasNodes {
Node * nodes();
};
class Node {
typedef int ValueType;
};
So far so good.
But now I need HasNodes to use the ValueType typedef, like so:
class HasNodes {
Node * nodes();
Node::ValueType* values();// Boom! "Use of undefined type Node" -- VC7
};
|
Is it possible to use anything in private area of the class directly?
You can use separate namespace to attain such code styling, that suits
here exactly.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kwikius Guest
|
Posted: Mon Jul 18, 2005 12:58 pm Post subject: Re: forward declaration using nested typedef |
|
|
[email]unspammable (AT) gmail (DOT) com[/email] wrote:
| Quote: | What I have is the following class hierarchy:
|
The trick is to prevent premature evaluation of the return type.
One way I can think of to do this is via template mechanism :
template<typename T>
class HasNodes{
T * nodes();
//not evaluated until argument supplied
typename T::ValueType* values();
};
class Node { typedef int ValueType; };
HasNodes<Node> nn;
Andy Little
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Stephan Brönnimann Guest
|
Posted: Mon Jul 18, 2005 1:01 pm Post subject: Re: forward declaration using nested typedef |
|
|
[email]unspammable (AT) gmail (DOT) com[/email] wrote:
| Quote: | What I have is the following class hierarchy:
class Node;
class HasNodes {
Node * nodes();
};
class Node {
typedef int ValueType;
};
So far so good.
But now I need HasNodes to use the ValueType typedef, like so:
class HasNodes {
Node * nodes();
Node::ValueType* values();// Boom! "Use of undefined type Node" -- VC7
};
An obvious workaround is to move the typedef out of class Node.
But I'm somewhat partial to this style of typedefing:
class Node {
typedef int ValueType;
};
which allows me to write Node::ValueType and doesn't pollute the
namespace, as opposed to:
typedef int NodeValueType;
class Node {
NodeValueType v_;
};
Any suggestions?
|
Trivial solution:
Why do you not declare the class Node before HasNodes?
I guess you have good reason, 2 possible solutions that
come to my mind:
+ repeat the typedef:
class HasNodes {
typedef int ValueType;
ValueType* values();
};
and let the compiler complain if Node::ValueType and
HasNodes::ValueType are not compatible. In the implementation
you can write:
Node::ValueType* HasNodes::values()
{
ValueType* rc;
// ...
return rc;
}
+ Isolate the typedef into its own struct/class:
struct NodeBase {
typedef int ValueType;
};
Stephan Brönnimann
[email]broeni (AT) osb-systems (DOT) com[/email]
Open source rating and billing engine for communication networks.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|