| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 873 人关注过本帖
标题:问个异常规范的问题
只看楼主 加入收藏
songhuirong1
Rank: 2
等 级:论坛游民
帖 子:116
专家分:38
注 册:2010-6-15
结帖率:94.12%
收藏
已结贴  问题点数:20 回复次数:2 
问个异常规范的问题
我在vs 2008中写了个程序,其中用到了异常规范,但是vs 2008却忽略了异常规范。请问vs 2008支持标准C++的异常规范不?

IntArray.h文件如下:

#ifndef INTARRAY_H
#define INTARRAY_H

#include <iostream>
using namespace std;

class OutOfBoundsException
{
public:
    OutOfBoundsException(int);

    int offset() const;
private:
    int _offset;
};

inline OutOfBoundsException::OutOfBoundsException(int offset):
_offset(offset)
{}

inline int OutOfBoundsException::offset() const
{
    return _offset;
}

class IntArray
{
public:
    IntArray(int);
    IntArray(int *, int);
    IntArray(const IntArray &);
    ~IntArray();

    int size() const;
    IntArray &operator =(const IntArray &);
    bool operator ==(const IntArray &);
    int &operator [](int) const;
    friend ostream &operator <<(ostream &, const IntArray &);
private:
    int _size;
    int *pArray;
};

inline IntArray::IntArray(int size):
_size(size)
{
    pArray = new int[_size];

    for(int ix=0;ix<_size;++ix)
        pArray[ix] = 0;
}

inline IntArray::IntArray(int *p, int size):
_size(size)
{
    pArray = new int[_size];

    for(int ix=0;ix<_size;++ix)
        pArray[ix] = p[ix];
}

inline IntArray::IntArray(const IntArray &rhs):
_size(rhs._size)
{
    pArray = new int[_size];

    for(int ix=0;ix<_size;++ix)
        pArray[ix] = rhs.pArray[ix];
}

inline IntArray::~IntArray()
{
    delete [] pArray;
    pArray = 0;
}

inline int IntArray::size() const
{
    return _size;
}

inline IntArray &IntArray::operator =(const IntArray &rhs)
{
    if(this != &rhs)
    {
        delete [] pArray;

        int size = rhs._size;
        pArray = new int[size];

        for(int ix=0;ix<size;++ix)
            pArray[ix] = rhs.pArray[ix];
    }

    return *this;
}

inline bool IntArray::operator ==(const IntArray &rhs)
{
    if(_size != rhs._size)
        return false;
    else
    {
        int ix = 0;

        for(;ix<_size;++ix)
        {
            if(pArray[ix] != rhs.pArray[ix])
                break;
        }

        if(ix < _size)
            return false;
        else
            return true;
    }
}

inline int &IntArray::operator [](int offset) const throw(OutOfBoundsException)
{
    if(offset >= _size)
        throw OutOfBoundsException(offset);
    else
        return pArray[offset];
}

#endif


IntArray.cpp文件如下:


#include "IntArray.h"
#include <iostream>
#include <fstream>
using namespace std;

ostream &operator <<(ostream &os, const IntArray &intArray)
{
    for(int ix=0;ix<intArray._size;++ix)
        cout << intArray[ix] << " ";
   
    return os;
}

int main()
{
    int a[] = {35,16,31,47,33,74,21,57,21,80};
    IntArray intArray(a,sizeof(a)/sizeof(a[0]));
    cout << intArray << endl;

    try
    {
        cout << intArray[10] << endl;
    }
    catch(OutOfBoundsException &oobe)
    {
        cerr << "下标[" << oobe.offset() << "]已经超出了数组的界限!" << endl;
    }
    return 0;
}

但是编译有警告,编译信息如下:


1>------ 已启动生成: 项目: CP11, 配置: Debug Win32 ------
1>正在编译...
1>IntArray.cpp
1>f:\program files\source\cp11\cp11\intarray.h(119) : warning C4290: 忽略 C++ 异常规范,但指示函数不是 __declspec(nothrow)
1>生成日志保存在“file://f:\Program Files\source\CP11\CP11\Debug\BuildLog.htm”
1>CP11 - 0 个错误,1 个警告
========== 生成: 成功 1 个,失败 0 个,最新 0 个,跳过 0 个 ==========
搜索更多相关主题的帖子: public include private 异常 
2010-12-29 21:43
最近不在
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:5
帖 子:204
专家分:842
注 册:2010-2-28
收藏
得分:10 
程序代码:
// C4290.cpp
// compile with: /EHs /W3 /c
void f1(void) throw(int) {}   // C4290

// OK
void f2(void) throw() {}
void f3(void) throw(...) {}


Visual C++ departs from the ANSI Standard in its implementation of exception specifications. The following table summarizes the Visual C++ implementation of exception specifications:
我也不太清楚,copy了关键语句,另外有个关于编译器开启异常的设置,详见
http://msdn.(v=VS.80).aspx
需要知道的是微软在某些地方做了不同处理,与标准c++有所区别.记得以前就遇到过new的异常抛出处理,跟编译器版本,而且又跟标准c++不太一样.

[ 本帖最后由 最近不在 于 2010-12-30 00:29 编辑 ]
2010-12-30 00:26
pangding
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:北京
等 级:贵宾
威 望:94
帖 子:6784
专家分:16751
注 册:2008-12-20
收藏
得分:10 
看了楼主发的另一个帖子,其实反而使我感觉微软的vs2008对标准异常的支持比我想像的要好得多的多。
楼主不用太纠结于这个了,有个 Warning 已经很管用了。忽略这个错误,一般来说可能能使 vs写的代码兼容性更好一点。
2010-12-31 19:54
快速回复:问个异常规范的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020866 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved