Tyl*_*lly 0 c++ implementation templates scope-resolution data-structures
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();
};
};
}
Run Code Online (Sandbox Code Playgroud)
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();
};
};
}
Run Code Online (Sandbox Code Playgroud)
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!
如果有任何改变,我正在使用视觉工作室......
鉴于:
namespace foo{
class A {
public:
class B {
B();
int size();
const int last();
};
};
}
Run Code Online (Sandbox Code Playgroud)
size 或 last 的函数定义的完整名称是:
int foo::A::B::size() {...}
const int foo::A::B::last() {...}
Run Code Online (Sandbox Code Playgroud)
鉴于:
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 & );
};
};
}
Run Code Online (Sandbox Code Playgroud)
函数定义将是:
template <typename T> int foo::A<T>::B::size() { ... }
template <typename T> const int foo::A<T>::B::last() { ... }
Run Code Online (Sandbox Code Playgroud)
对于这些,获取指向成员函数的指针将是:
auto p = &foo::A<T>::B::size;
Run Code Online (Sandbox Code Playgroud)
构造函数定义将是:
template<typename T> foo::A<T>::B::B() {}
Run Code Online (Sandbox Code Playgroud)
做这些事情之一:
foo::A<T>::B nb{}; // note, with nb() it complains
Run Code Online (Sandbox Code Playgroud)
操作符函数定义返回对 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&
Run Code Online (Sandbox Code Playgroud)
如果你好奇,如果模板函数在 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 }
Run Code Online (Sandbox Code Playgroud)