请使用以下代码;
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&.
问题 - 应该吗?:-)
使用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++,我很难理解为什么这段代码会崩溃.我已经确定这一行: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) 如何string在不使用strtok或istringstream找到最大的单词的情况下将单词拆分并存储在单独的数组中?我只是一个初学者,所以我应该做到这一点使用基本功能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)