简体   繁体   中英

How to write the scope resolution operator function header for nested classes?

Hey I have a fairly simple question that some quick google searches couldnt solve so I'm coming here for some help.

I'm having trouble just getting my assignment off the ground because I can't even write the skeleton code!

Basically I have a header file like so:

namespace foo{
    class A {
    public:
        class B {
            B(); 
            int size();
            const int last();
        };
    };
}

And I want to know how to refer to these guys outside of the file in a implementation file.

BONUS:

namespace foo{

    template<typename T>
    typename
    class A {
    public:
        class B {
            B(); 
            int size();
            const int last();
        };
    };
}

how are these functions referred to as?

Is there a formula I can follow when it comes to this or is it more of flexible, different for your needs kinda thing?

Thanks for the help!

I'm using visual studios if that changes anything...

Given:

namespace foo{
    class A {
    public:
        class B {
            B(); 
            int size();
            const int last();
        };
    };
}

The complete name for a function definition of size or last would be:

int foo::A::B::size() {...}
const int foo::A::B::last() {...}

Given:

namespace foo{

    template<typename T>
    typename
    class A {
    public:
        class B {
            B(); 
            B & operator ++();
            int size();
            const int last();

            template< typename I, typename R>
            R getsomethingfrom( const I & );
        };
    };
}

The function definitions would be:

template <typename T> int foo::A<T>::B::size() { ... }
template <typename T> const int foo::A<T>::B::last() { ... }

For these, getting the pointer to a member function would be:

auto p = &foo::A<T>::B::size;

Constructor definition would be:

template<typename T> foo::A<T>::B::B() {}

Making one of these things:

foo::A<T>::B nb{}; // note, with nb() it complains

The operator function definition returning a reference to a B, in the template, tricky:

template<typename T>         // standard opening template....
typename foo::A<T>::B &        // the return type...needs a typename 
foo::A<T>::B::operator++()     // the function declaration of operation ++
{ ... return *this; }        // must return *this or equivalent B&

In case you're curious, if a template function is inside B, like getsomethingfrom, then definition of the function is:

template< typename T>                       // class template
template< typename I, typename R>           // function template
R foo::A<T>::B::getsomethingfrom( const I & ) // returns an R, takes I
{ R r{}; return r }

To use the class in your implementation (.cpp) file you go likes that:

namespace foo{
    A::B::B(){
        // this is your inner class's constructor implementation
    }

    int A::B::size(){
        // implementation for you size()
        int res = last(); // access the method last() of the same object
        return res;
    }

    const int A::B::last(){
        // implementation of last()
        return 42;
    }

}

void main(){
    foo::A a; // construct an object of A
    // can't do anything useful as the methods of A::B are all private
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM