Vito Carleone
Vito Carleone

Reputation: 167

notion of a current object of the enclosing class

I m studying Bjarne's book and look for Member Types of Class.
A nested class has access to members of its enclosing class, even to private members (just as a member function has), but has no notion of a current object of the enclosing class.
But why I dont get any error when I build following code.

template<typename T>
class Tree
{
private:
    using value_type = T;

    class Node
    {
    private:
        Node* right;
        value_type value;
    public:
        void Node_Function(Tree*);
    };
    Node* top;

public:
    void X_f()
    {

    }
};

template<typename T>
void Tree<T>::Node::Node_Function(Tree* p)
{
    top = right; //I suppose to get error here like "error : 
                 // no object of type Tree specified"
}

Upvotes: 2

Views: 123

Answers (2)

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361582

There is one rule that dictates that if a member of class template is not used, then that member is not instantiated. That applies to your situation.

You're not calling Node_Function(), so the compiler doesn't instantiate it, hence it doesn't see the problem. The function is still parsed for syntax check which is correct — it doesn't attempt to know what right is, as it could be a variable, a function name, anything.

Here is a demo which gives error on calling it.

Upvotes: 2

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385224

You haven't attempted to use that function template, so nothing happens.

If you'd debugged with and provided a testcase with a main function invoking this problematic code, you'd have seen the error message you seek.

- Like this…

Upvotes: 2

Related Questions