标题:问个异常规范的问题
取消只看楼主
songhuirong1
Rank: 2
等 级:论坛游民
帖 子:116
专家分:38
注 册:2010-6-15
结帖率:94.12%
已结贴  问题点数:20 回复次数:0 
问个异常规范的问题
我在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



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-330244-1-1.html




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

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