亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 4425 | 回復(fù): 2
打印 上一主題 下一主題

[C++] (請教)繼承普通類和模板類的一個問題(關(guān)于類作用域) [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2011-07-08 16:19 |只看該作者 |倒序瀏覽
請看代碼

1:  普通類---------------------------------
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
using namespace std;

class A
{
    int a;
public:
    A(int aa = 19) : a(aa){ }
    int get_value_A() const { return a; }
};

class B: public A
{
public:
    B() : A(40) { }

    int get_value()
    {
        return get_value_A();
    }
};

int main(int argc, char* argv[])
{
    B b;
    printf("%d\n", b.get_value());

    return 0;
}

2: 模板類 -------------------------頭文件----inherit_tem.h-------
#ifndef _AAAAAAAAAAA_
#define _AAAAAAAAAAA_
#include <iostream>
using namespace std;
template<class T, int ret = 6>
class A
{
    T a;
    int b;
public:
    A(T& aa = 0) : a(aa), b(ret){ }
    void print() const
    {
        cout << "A: " << a << endl;
    }
    int count() const{ return b; }
};


template<class T>
class B : public A<T>
{
public:
    B(T& aa) : A<T>(aa){ }
    void Bprint() const
    {
        A<T>::print();
    }
    void print_int() const
    {
        cout << "print_int " << A<T>::count() << endl;
    }
};

#endif
-------------------源文件------------------
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "inherit_tem.h"

using namespace std;

int main(int argc, char* argv[])
{
    int bb = 3;
    B<int> b(bb);
    b.Bprint();
    b.print_int();

    return 0;
}
.....................................................................
........................................................................
在繼承模板類中 子類調(diào)用父類函數(shù) 為什么必須要加 父類作用域 A<T>::  否則編譯不過  ,而 普通類繼承卻不用加父類作用域
請教  我用的是g++ 4.4.5   GNU project C++ compiler

論壇徽章:
0
2 [報告]
發(fā)表于 2011-07-09 09:35 |只看該作者
這的確是個問題啊

論壇徽章:
0
3 [報告]
發(fā)表于 2011-07-09 15:03 |只看該作者
引用一下我博客上某篇文章的一段話:
在模板定義中,出現(xiàn)的名字可分為三類:
    (1)模板自身的名字和在模板中聲明的名字;
    (2)依賴于模板參數(shù)的名字(dependent name);
    (3)不依賴于模板參數(shù)且在模板定義中可見的名字(non-dependent name)。
在名字解析時,第二類名字和第三類名字的解析綁定不同,被稱為二段式名字查找規(guī)則。具體而言,對于dependent name,由于模板參數(shù)類型未知,因此其只能在模板實例化的上下文中才能進行名字查找與綁定。對于non-dependent name,其在名字出現(xiàn)的地方采用一般的名字查找規(guī)則。

http://blogold.chinaunix.net/u/18517/showart_454524.html
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP