C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

forward declaration using nested typedef

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
unspammable@gmail.com
Guest





PostPosted: Sun Jul 17, 2005 8:21 pm    Post subject: forward declaration using nested typedef Reply with 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?


[ 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





PostPosted: Mon Jul 18, 2005 8:19 am    Post subject: Re: forward declaration using nested typedef Reply with quote



[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.

Quote:
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
kwikius
Guest





PostPosted: Mon Jul 18, 2005 12:58 pm    Post subject: Re: forward declaration using nested typedef Reply with quote





[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





PostPosted: Mon Jul 18, 2005 1:01 pm    Post subject: Re: forward declaration using nested typedef Reply with quote

[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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.