Re: Getting non "const" pointers to object data using "const" members

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



"Ulrich Eckhardt" <eckhardt@xxxxxxxxxxxxxx> a écrit dans le message de news:lmgm96-su7.ln1@xxxxxxxxxxxxxxxxxxxxxxxxx
Gingko wrote:

The returned pointers are not arbitrary, they are *selected* parts of the
object, other parts are still expected to be not accessible.

My object is actually a tree container, and the member function explores
all branches for returning a flattened list of pointers to all leaves.

There are a few things you can do to achieve that, while not violating type
safety or const correctness.

1. Separate the container part from the content:
template<typename element_type>
struct node
{
element_type value;
node* parent;
node* left;
node* right;
};
Make this a private nested class of the tree container.

2. Provide an interface to access the elements of the tree. One such
interface is an iterator (take a look at Boost's iterator builder library).
Another such interface is a visitor:

struct tree {
...
template<typename Visitor>
void visit(Visitor v);
template<typename Visitor>
void visit(Visitor v) const;
};

My class system already more or less looks like that
But what is "Visitor v" ? A container for the flattened list ? Shouldn't it be more likely something like "Visitor & v" ?

Note that this is not exactly the visitor pattern, since that is intended to
do double dispatch rather. The function above will invoke "v(e)" for every
node's value 'e'. This allows you to e.g. create a flat list of all nodes,
but the list will point to const objects when you use the const overload of
this function. Note that I would _not_ call it with every node but only its
value, because the node is an internal thing to the tree.

How do you want the node to be modifiable (which is a primary requirement) from the flattened list if the node is const or passed by value ?

Leaves are expected to be modifiable (and randomly accessible like in a
subscripted array), but I want the structure of the tree itself to be
protected.

You really support 'tree[index]'? That tempts to write horribly inefficient
code like using a 'for(int i=0; ...)' loop to iterate the tree.

I am actually trying to implement it as a std::vector<Node *>, filled by pointers without ownership.

I use a polymorphic member actually truely named "visit", which is a "const" member implemented either for getting the pointer in the node itself or in another class which is a sub-container for several nodes or nodes containers (in that case, "visit" will recursively iterate for calling the "visit" member of all of these nodes).

This polymorphic member has a "std::vector<Node *> & vNodeList" as argument.

The only problem is, as "visit" is a "const" member, which seems more or less logical to me, it does not allow "vNodeList.push_back(this)" for getting the pointer.
This will probably end by removing the "const"'ness of all "visit" members, but I don't think that this will fully satisfy me.

Gilles

.



Relevant Pages

  • Re: design dilemma
    ... > state variable is exposed only through a const object... ... return a pointer (or iterator) to another object, ... So, when you call begin on a const container you get a const iterator, ... class to access a private member variable. ...
    (comp.object)
  • Re: The reality of Topmind (Was: Topic-Organized Object-Oriented Programming)
    ... unless the tree correction was in the first ... It is, simply, classifying all of a class' member variables and member ... In the *5th message* in the entire topic thread, ...
    (comp.object)
  • Re: Is a number an object?
    ... If I have one tree in my garden, and the emporer has one tree in his ... the difference between it an its surronding, ... The surroundings must be themselves identifiable or we would not be able to count anything in it. ... member, there is one } in any set that contain } as a member. ...
    (sci.logic)
  • Re: Cantor Confusion
    ... The successor of every member of S is a member of S ... but the union itself may be a non-natural number ... And how about the paths of a tree? ... There is a finite binary index to any listed sequence of the form ...
    (sci.math)
  • Re: HELP DISTINGUISHED NAME DN IN AD (WIN2000 SERVER)
    ... object resides in an OU tree, which in turn resides in a ... domain tree, a DN specifies the exact location within both trees. ... An RDN has to be unique within it's container. ...
    (microsoft.public.win2000.active_directory)