鱼C论坛

 找回密码
 立即注册
查看: 1382|回复: 6

[已解决]关于模板结构体的问题

[复制链接]
发表于 2021-8-13 15:24:55 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
请看下面的代码:
template <class T>
struct point{
        inline point():x(0),y(0) {}
        inline point(T _x, T _y):x(_x),y(_y){}
        T x, y;
};

有两个问题
(1)为什么在结构体point中还要内联两个point,这样做的作用是什么?
(2)在point结构体中内联point,这该怎么理解?
最佳答案
2021-8-13 17:53:09
闪亮的马路 发表于 2021-8-13 17:46
inline orientedpoint(T x, T y, A _theta): point(x,y), theta(_theta){}
上面这一句后面是没有分号的 ...

什么错?
分号想加几个就加几个的说
template <class T>
struct point{
        inline point():x(0),y(0) {}
        inline point(T _x, T _y):x(_x),y(_y){}
        T x, y;
};

template <class T, class A>
struct orientedpoint: public point<T>{
    inline orientedpoint() : point<T>(0,0), theta(0) {};;;;;;;;;;;;;;;;;;;;;;
    inline orientedpoint(const point<T>& p);
    inline orientedpoint(T x, T y, A _theta): point<T>(x,y), theta(_theta){}
    inline void normalize();
    inline orientedpoint<T,A> rotate(A alpha){
        T s=sin(alpha), c=cos(alpha);
        A a=alpha+theta;
        a=atan2(sin(a),cos(a));
        return orientedpoint(
                c*this->x-s*this->y,
                s*this->x+c*this->y,
                a);
    }
    A theta;
};

int main(void) {
    orientedpoint<int, int> o;
    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-8-13 15:27:36 | 显示全部楼层
再补充一个:
template <class T, class A>
struct orientedpoint: public point<T>{
  inline orientedpoint() : point<T>(0,0), theta(0) {};
        inline orientedpoint(const point<T>& p);
        inline orientedpoint(T x, T y, A _theta): point<T>(x,y), theta(_theta){}
        inline void normalize();
        inline orientedpoint<T,A> rotate(A alpha){
                T s=sin(alpha), c=cos(alpha);
                A a=alpha+theta;
                a=atan2(sin(a),cos(a));
                return orientedpoint(
                        c*this->x-s*this->y,
                        s*this->x+c*this->y,
                        a);
        }
        A theta;
};
这里也有很多内联,且是不同形式的,有哪位大神给解释一下
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-13 15:28:16 | 显示全部楼层
不是内联两个 point 对象,这两个是构造函数
是用来创建 point 这个对象用的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-8-13 17:46:33 | 显示全部楼层
人造人 发表于 2021-8-13 15:28
不是内联两个 point 对象,这两个是构造函数
是用来创建 point 这个对象用的

inline orientedpoint(T x, T y, A _theta): point<T>(x,y), theta(_theta){}
上面这一句后面是没有分号的,是写错了,还是怎样?这是源码,不应该错误呢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-13 17:53:09 | 显示全部楼层    本楼为最佳答案   
闪亮的马路 发表于 2021-8-13 17:46
inline orientedpoint(T x, T y, A _theta): point(x,y), theta(_theta){}
上面这一句后面是没有分号的 ...

什么错?
分号想加几个就加几个的说
template <class T>
struct point{
        inline point():x(0),y(0) {}
        inline point(T _x, T _y):x(_x),y(_y){}
        T x, y;
};

template <class T, class A>
struct orientedpoint: public point<T>{
    inline orientedpoint() : point<T>(0,0), theta(0) {};;;;;;;;;;;;;;;;;;;;;;
    inline orientedpoint(const point<T>& p);
    inline orientedpoint(T x, T y, A _theta): point<T>(x,y), theta(_theta){}
    inline void normalize();
    inline orientedpoint<T,A> rotate(A alpha){
        T s=sin(alpha), c=cos(alpha);
        A a=alpha+theta;
        a=atan2(sin(a),cos(a));
        return orientedpoint(
                c*this->x-s*this->y,
                s*this->x+c*this->y,
                a);
    }
    A theta;
};

int main(void) {
    orientedpoint<int, int> o;
    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-8-16 10:16:43 | 显示全部楼层
人造人 发表于 2021-8-13 17:53
什么错?
分号想加几个就加几个的说

想加几个就加几个,但是我说的源码是没有分号的,没有分号这是什么情况?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-16 12:59:34 | 显示全部楼层
闪亮的马路 发表于 2021-8-16 10:16
想加几个就加几个,但是我说的源码是没有分号的,没有分号这是什么情况?

分号根据语法规则,随便加
下面这个程序没问题
#include <stdio.h>
;;;;
;;;
;;;int main(void) {;;;;;
    ;;;;;
    ;
    ;;;;;printf("hello world!\n");;;;
    ;;;
    ;;;;return 0;;;
};;;;;
;;;;;
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-22 01:18

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表