小编And*_*zej的帖子

奇怪的C++模板和const问题

我不明白为什么这个程序的输出是第二种方法而不是第一种方法 ......

#include <iostream>

template <class T>
void assign(T& t1,T& t2){
    std::cout << "First method"<< std::endl;
}

template <class T>
void assign(T& t1,const T& t2) {
    std::cout << "Second method"<< std::endl;
}

class A
{
public:
    A(int a):_a(a){};
private:
    int _a;
    friend A operator+(const A& l, const A& r);
};

A operator+(const A& l, const A& r) {
friend A operator+(const A& l, const A& r);return A(l._a+r._a);
}

int main ()
{
    A a=1;
    const A b=2; …
Run Code Online (Sandbox Code Playgroud)

c++ templates

7
推荐指数
2
解决办法
424
查看次数

C char []交换问题

我有这个简短的代码:

#include <stdio.h>
void fastSwap (char **i, char **d)
{
    char *t = *d;
    *d = *i;
    *i = t;
}
int main ()
{
    char num1[] = "hello";
    char num2[] = "class";
    fastSwap ((char**)&num1,(char**)&num2);
    printf ("%s\n",num1);
    printf ("%s\n",num2);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这个简短程序的输出是:

claso
hells
Run Code Online (Sandbox Code Playgroud)

我只是不明白为什么每个char []的最后一个字母都被交换了.有任何想法吗?

c swap char

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

标签 统计

c ×1

c++ ×1

char ×1

swap ×1

templates ×1