只是偶然发现了一些我无法解释的事情.以下代码无法编译
template<int a>
class sub{
protected:
int _attr;
};
template<int b>
class super : public sub<b>{
public:
void foo(){
_attr = 3;
}
};
int main(){
super<4> obj;
obj.foo();
}
Run Code Online (Sandbox Code Playgroud)
而当我改变_attr = 3;到this->attr = 3;那里似乎没有问题.
这是为什么?有什么情况你必须使用它吗?
我曾经g++ test.cpp -Wall -pedantic编译,我得到以下错误
test.cpp: in member function 'void super<b>::foo()':
test.cpp:11:3: error: '_attr' was not declared in this scope
Run Code Online (Sandbox Code Playgroud) 假设我有以下文件结构
\n\n. \n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 docker-compose.yml \n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 webserver \n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 Dockerfile \n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 html \n \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 test1 \n \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 index.html \n \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 test2 \n \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 index.html \n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 vhosts.conf \n\n4 directories, 5 files\nRun Code Online (Sandbox Code Playgroud)\n\n和docker-compose.yml:
version: "3.1"\nservices:\n\n webserver:\n build: webserver\n container_name: web\n working_dir: /application\n volumes:\n - ./webserver/html/:/application\n ports:\n - "80:80"\n\n other:\n image: php:7.2.7\n container_name: other\n tty: true\nRun Code Online (Sandbox Code Playgroud)\n\n和Dockerfile:
FROM php:7.2.7-apache\n\nCOPY vhosts.conf /etc/apache2/sites-enabled/000-default.conf\nRun Code Online (Sandbox Code Playgroud)\n\n和vhosts.conf:
<VirtualHost *:80>\n …Run Code Online (Sandbox Code Playgroud)