简体   繁体   English

如何为嵌套类编写范围解析运算符函数标头?

[英]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. 嘿,我有一个相当简单的问题,一些快速的Google搜索无法解决,所以我来这里寻求帮助。

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: size或last的函数定义的完整名称为:

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: 在模板中,操作符函数定义返回对B的引用比较棘手:

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: 如果您很好奇,如果模板函数在B内部(如getsomethingfrom),则该函数的定义为:

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: 要在实现(.cpp)文件中使用该类,您需要这样:

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++二进制范围解析运算符和类 - C++ Binary Scope Resolution Operator and Classes 使用对象而不是范围解析运算符(::)访问类中的typedef - Access typedef in classes with an object, not the scope resolution operator (::) Scope 解析运算符,用于返回嵌套的 class 类型 - Scope resolution operator for returning a nested class type 关于嵌套类的operator new的重载决策 - Overload resolution of operator new regarding nested classes 通过范围解析运算符arduino传递函数指针 - Passing function pointer with scope resolution operator arduino 函数模板中的decltype和scope resolution运算符 - decltype and scope resolution operator inside a function template 如何在C ++中没有作用域解析运算符的情况下访问标头成员类型? - How to access a header member type without scope resolution operator in C++? 如何理解使用 ::(范围解析运算符)访问类内类(嵌套类)或 typedef - How to understand Using :: (Scope resolution operator) to access a in-class class (nested class) or typedef 如何在模板 class 中使用 scope 分辨率运算符用于嵌套的 class? - How do I use the scope resolution operator for a nested class inside a template class? 如何使用 scope 分辨率运算符调用方法 - How to call method using scope resolution operator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM