小编Gri*_*ner的帖子

抛出异常为const&

请使用以下代码;

void DoThrow( const std::exception& e )
{
    throw e;
}

int main( int nArgs, char* args[] )
{
    std::exception e;
    try
    {
        DoThrow( e );
    }
    catch( std::exception& e )
    {
        // const exception ref is caught
    }


    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试在我的项目中改进const正确性并且无意中创建了上述情况.就目前而言,在Dev Studio中,catch块可以捕获异常,尽管事实上它被抛出为const&但是被捕获为非const&.

问题 - 应该吗?:-)

c++ const exception

7
推荐指数
1
解决办法
239
查看次数

使用ptr-ref类型和专业化规则的模板专业化

使用MSVC 2010,我得到以下行为:

template <class T> class Boogy
{
public:
    void Fn( T in )
    {
    }

    void Fn2( const T& in)
    {
    }
};

template <> void Boogy<int>::Fn( int in ) //builds ok
{
}

template <> void Boogy<int*>::Fn( int* in ) //builds ok
{
}

template <> void Boogy<int>::Fn2( const int& in ) //builds ok
{
}

template <> void Boogy<int*>::Fn2( const int*& in ) //DOES NOT BUILD
{
}

typedef int* intStar;
template <> void Boogy<intStar>::Fn2( const intStar& …
Run Code Online (Sandbox Code Playgroud)

c++ templates template-specialization

2
推荐指数
1
解决办法
123
查看次数

简单的递归C++代码不断崩溃

所以我在教自己C++,我很难理解为什么这段代码会崩溃.我已经确定这一行:string str = to_string(n)可能不正确.但我没有看到其他错误导致它崩溃的原因.

#include <iostream>
#include <string>

using namespace std;

void write_vertically(int n)
{
    string str = to_string(n);

    if (str.length()>=0) 
    {
        cout<<stoi(str.substr(0,1))<<endl;
        write_vertically(stoi(str.substr(1,str.length())));
    }

}

int main( ) 
{
    write_vertically(1234567890);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ recursion iostream

2
推荐指数
1
解决办法
169
查看次数

如何从字符串中提取单词并将它们存储在c ++中的不同数组中

如何string在不使用strtokistringstream找到最大的单词的情况下将单词拆分并存储在单独的数组中?我只是一个初学者,所以我应该做到这一点使用基本功能string.h中一样strlen,strcpy只有等.有可能吗?我试图这样做,我发布了我所做的.请纠正我的错误.

#include<iostream.h>
#include<stdio.h>
#include<string.h>
void count(char n[])
{
    char a[50], b[50];
    for(int i=0; n[i]!= '\0'; i++)
    {
        static int j=0;
        for(j=0;n[j]!=' ';j++)
        {
            a[j]=n[j];
        }
        static int x=0;
        if(strlen(a)>x)
        {
            strcpy(b,a);
            x=strlen(a);
        }
    }
    cout<<"Greatest word is:"<<b;
}

int main( int, char** )
{
    char n[100];
    gets(n);
    count(n);
}
Run Code Online (Sandbox Code Playgroud)

c++ string split

0
推荐指数
1
解决办法
9102
查看次数