鱼C论坛

 找回密码
 立即注册
查看: 1603|回复: 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){}
上面这一句后面是没有分号的 ...

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

  7. template <class T, class A>
  8. struct orientedpoint: public point<T>{
  9.     inline orientedpoint() : point<T>(0,0), theta(0) {};;;;;;;;;;;;;;;;;;;;;;
  10.     inline orientedpoint(const point<T>& p);
  11.     inline orientedpoint(T x, T y, A _theta): point<T>(x,y), theta(_theta){}
  12.     inline void normalize();
  13.     inline orientedpoint<T,A> rotate(A alpha){
  14.         T s=sin(alpha), c=cos(alpha);
  15.         A a=alpha+theta;
  16.         a=atan2(sin(a),cos(a));
  17.         return orientedpoint(
  18.                 c*this->x-s*this->y,
  19.                 s*this->x+c*this->y,
  20.                 a);
  21.     }
  22.     A theta;
  23. };

  24. int main(void) {
  25.     orientedpoint<int, int> o;
  26.     return 0;
  27. }
复制代码
小甲鱼最新课程 -> https://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;
};
这里也有很多内联,且是不同形式的,有哪位大神给解释一下
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-13 15:28:16 | 显示全部楼层
不是内联两个 point 对象,这两个是构造函数
是用来创建 point 这个对象用的
小甲鱼最新课程 -> https://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){}
上面这一句后面是没有分号的,是写错了,还是怎样?这是源码,不应该错误呢
小甲鱼最新课程 -> https://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){}
上面这一句后面是没有分号的 ...

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

  7. template <class T, class A>
  8. struct orientedpoint: public point<T>{
  9.     inline orientedpoint() : point<T>(0,0), theta(0) {};;;;;;;;;;;;;;;;;;;;;;
  10.     inline orientedpoint(const point<T>& p);
  11.     inline orientedpoint(T x, T y, A _theta): point<T>(x,y), theta(_theta){}
  12.     inline void normalize();
  13.     inline orientedpoint<T,A> rotate(A alpha){
  14.         T s=sin(alpha), c=cos(alpha);
  15.         A a=alpha+theta;
  16.         a=atan2(sin(a),cos(a));
  17.         return orientedpoint(
  18.                 c*this->x-s*this->y,
  19.                 s*this->x+c*this->y,
  20.                 a);
  21.     }
  22.     A theta;
  23. };

  24. int main(void) {
  25.     orientedpoint<int, int> o;
  26.     return 0;
  27. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

想加几个就加几个,但是我说的源码是没有分号的,没有分号这是什么情况?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

分号根据语法规则,随便加
下面这个程序没问题
  1. #include <stdio.h>
  2. ;;;;
  3. ;;;
  4. ;;;int main(void) {;;;;;
  5.     ;;;;;
  6.     ;
  7.     ;;;;;printf("hello world!\n");;;;
  8.     ;;;
  9.     ;;;;return 0;;;
  10. };;;;;
  11. ;;;;;
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-26 21:54

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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