当const进入类和对象家里

本文详细介绍了C++中const限定符的三种使用场景:修饰类的成员变量、修饰对象以及修饰类的成员方法,并解释了每种情况下const的作用及注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

当const与类和对象对眼时,会产生三种情况:

1、const修饰类的成员变量:

class A
{
public:
	A(int b):ma(b){}
private:
	const int ma ;
};

 

      首先,const修饰类的成员变量是带常性的,所以它必须在定义时初始化。而类只有在实例化对象时才会对成员变量进行初始化。所以const修饰类的成员变量时,必须依靠构造函数的初始化列表来对它进行初始化。

2、const修饰对象:

  定义常对象的一般形式为

  类名    const    对象名(实参列表);

  const    类名    对象名(实参列表);

      const修饰的对象是常对象,其内容不可变。而且常对象只能调用常方法(这样做的原因是担心非常方法可能会修改常对象数据)。有时候有需求修改常对象里的chen成员变量,可以在该成员变量定义时加上mutable关键字,声明它是可修改的。

3、const修饰类的成员方法

      const修饰类的成员方法又叫常方法,个人认为它就是为了访问常对象里的数据而生成的。它只能访问常对象的成员变量,而不能做修改(mutable关键字修饰的例外)。

class A
{
public:
	A(int b):ma(b){}
	void Print() //const
	{
		cout<<ma<<endl;
	}
private:
	int ma ;
};

int main()
{
	const A b(1);
	b.Print();
}

这里屏蔽掉成员方法后的const则会出现“A::Print”: 不能将“this”指针从“const A”转换为“A &”的错误。如果此时的Print函数去试图修改成员变量的值,也不会被允许。值得提醒的是:普通对象可以调用常方法。

总结:1、const修饰一个变量时,它会给该变量加上常性,不能被修改。

           2、访问和修改的关系图。

&lt;template&gt; &lt;div class=&quot;scroll-container&quot; ref=&quot;container&quot;&gt; &lt;section v-for=&quot;(section, index) in sections&quot; :key=&quot;index&quot; :class=&quot;[{ active: currentIndex === index }, section.bgClass]&quot; &gt; &lt;div class=&quot;section section-one&quot; v-if=&quot;index === 0&quot;&gt; &lt;div class=&quot;title&quot;&gt;{{ section.title }}&lt;/div&gt; &lt;img src=&quot;./img/phone.png&quot; alt=&quot;&quot; /&gt; &lt;div class=&quot;content&quot;&gt;{{ section.content }}&lt;/div&gt; &lt;/div&gt; &lt;div class=&quot;section section-two&quot; v-if=&quot;index === 1&quot;&gt; &lt;div class=&quot;title&quot;&gt;{{ section.title }}&lt;/div&gt; &lt;div class=&quot;content&quot;&gt;{{ section.content }}&lt;/div&gt; &lt;/div&gt; &lt;div class=&quot;section section-three&quot; v-if=&quot;index === 2&quot;&gt; &lt;div&gt; &lt;img src=&quot;./img/phone3.png&quot; alt=&quot;&quot; /&gt; &lt;/div&gt; &lt;div&gt; &lt;div class=&quot;title&quot;&gt;{{ section.title }}&lt;/div&gt; &lt;div class=&quot;content&quot;&gt;{{ section.content }}&lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class=&quot;section section-four&quot; v-if=&quot;index === 3&quot;&gt; &lt;div&gt; &lt;img src=&quot;./img/phone4.png&quot; alt=&quot;&quot; /&gt; &lt;/div&gt; &lt;div&gt; &lt;div class=&quot;title&quot;&gt;{{ section.title }}&lt;/div&gt; &lt;div class=&quot;content&quot;&gt;{{ section.content }}&lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class=&quot;section section-five&quot; v-if=&quot;index === 4&quot;&gt; &lt;div class=&quot;title&quot;&gt;{{ section.title }}&lt;/div&gt; &lt;img src=&quot;./img/phone5.png&quot; alt=&quot;&quot; /&gt; &lt;div class=&quot;content&quot;&gt;{{ section.content }}&lt;/div&gt; &lt;/div&gt; &lt;/section&gt; &lt;!-- 右侧导航圆点 --&gt; &lt;div class=&quot;nav-dots&quot;&gt; &lt;div v-for=&quot;(dot, index) in sections&quot; :key=&quot;index&quot; class=&quot;dot&quot; :class=&quot;{ active: currentIndex === index }&quot; @click=&quot;scrollToSection(index)&quot; &gt;&lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/template&gt; &lt;script setup&gt; import { ref, onMounted, // onUnmounted } from &quot;vue&quot;; // 页面内容数据 const sections = ref([ { title: &quot;土豆星球&quot;, content: &quot;现代人的生活压力通常很大,各行各业皆是如此,你的挚爱亲朋不理解你,你的公司领导瞧不上你,也许是你没能遇到一个懂你、理解你的知己,也许是你的脆弱不希望身边的人去诉说。来T土豆星球,进行一次心灵测试,我们来帮你找到那个懂你,倾听你的他/她,让相似经历的你们,彼此陪伴,伴你走出人生的低谷。&quot;, bgClass: &quot;bg1&quot;, }, { title: &quot;穿越过去&quot;, content: &quot;每个人都曾经历过痛苦的离别,每个人的内心深处都有很多来不及说出的话,时空胶囊能让你失去/逝去的人隔空对话,任何时间任何地点,点开土豆星球,打开时空胶囊,都能让你回到过去,跟爷爷奶奶说一句:&ldquo;家里一切都好,你们好吗?&rdquo; 跟父母说一句:&ldquo;我爱你们&rdquo;公司基于强大的技术实力元宇宙的星现形式,为你补完内心深处的遗憾。&quot;, bgClass: &quot;bg2&quot;, }, { title: &quot;心灵对话&quot;, content: &quot;在遭受困难时,你会不会感叹命运的不公?为什么别人过的那么好,而你却什么也不是?很多人因此失去了人生的目标对未来的希望。你是否希望未来的自己能穿越回现在给你指引方向?其实每个人都不普通,只是你还没找到自己的闪光点,让我们站在未来的高处,跨域时间现实的梏,让未来的你,伸手将今天的自己拉出困境与低谷。 &quot;, bgClass: &quot;bg3&quot;, }, { title: &quot;匹配社交&quot;, content: &quot;现代人的生活压力通常很大,各行各业皆是如此,你的挚爱亲朋不理解你,你的公司领导瞧不上你,也许是你没能遇到一个懂你、理解你的知己,也许是你的脆弱不希望身边的人去诉说。来T土豆星球,进行一次心灵测试,我们来帮你找到那个懂你,倾听你的他/她,让相似经历的你们,彼此陪伴,伴你走出人生的低谷。&quot;, bgClass: &quot;bg4&quot;, }, { title: &quot;寻找话题&quot;, content: &quot;我们每个人都是不同的个体,有不同的价值观,或许事情没有绝对的对错,只是缺少聊得来的人。网络虽然大,但依旧无法分享你喜欢的聊天话题,那些在你心里积攒了无数个日月的话,想找别人分享,但一直没有找到分享的对象,那么土豆星球是一个你能够展示自己的平台,有一群你志同道合的小伙伴在等着你!&quot;, bgClass: &quot;bg5&quot;, }, ]); const container = ref(null); const currentIndex = ref(0); let isScrolling = false; // 处理滚动事件 const handleWheel = (e) =&gt; { if (isScrolling) return; isScrolling = true; const direction = e.deltaY &gt; 0 ? 1 : -1; const newIndex = Math.min( Math.max(currentIndex.value + direction, 0), sections.value.length - 1 ); if (newIndex !== currentIndex.value) { currentIndex.value = newIndex; container.value.scrollTo({ top: window.innerHeight * newIndex, behavior: &quot;smooth&quot;, }); } setTimeout(() =&gt; { isScrolling = false; }, 800); }; // 添加键盘支持 const handleKeyDown = (e) =&gt; { if (e.key === &quot;ArrowDown&quot;) { currentIndex.value = Math.min( currentIndex.value + 1, sections.value.length - 1 ); } else if (e.key === &quot;ArrowUp&quot;) { currentIndex.value = Math.max(currentIndex.value - 1, 0); } container.value.scrollTo({ top: window.innerHeight * currentIndex.value, behavior: &quot;smooth&quot;, }); }; // 添加触摸滑动支持 let touchStartY = 0; const handleTouchStart = (e) =&gt; { touchStartY = e.touches[0].clientY; }; const handleTouchEnd = (e) =&gt; { const touchEndY = e.changedTouches[0].clientY; const diff = touchStartY - touchEndY; if (Math.abs(diff) &gt; 50) { const direction = diff &gt; 0 ? 1 : -1; const newIndex = Math.min( Math.max(currentIndex.value + direction, 0), sections.value.length - 1 ); if (newIndex !== currentIndex.value) { currentIndex.value = newIndex; container.value.scrollTo({ top: window.innerHeight * newIndex, behavior: &quot;smooth&quot;, }); } } }; const scrollToSection = (index) =&gt; { currentIndex.value = index; container.value.scrollTo({ top: window.innerHeight * index, behavior: &quot;smooth&quot;, }); }; // 生命周期钩子 onMounted(() =&gt; { window.addEventListener(&quot;wheel&quot;, handleWheel, { passive: false }); window.addEventListener(&quot;keydown&quot;, handleKeyDown); container.value.addEventListener(&quot;touchstart&quot;, handleTouchStart); container.value.addEventListener(&quot;touchend&quot;, handleTouchEnd); }); // onUnmounted(() =&gt; { // window.removeEventListener(&#39;wheel&#39;, handleWheel) // window.removeEventListener(&#39;keydown&#39;, handleKeyDown) // container.value.removeEventListener(&#39;touchstart&#39;, handleTouchStart) // container.value.removeEventListener(&#39;touchend&#39;, handleTouchEnd) // }) &lt;/script&gt; &lt;style scoped lang=&quot;scss&quot;&gt; .scroll-container { height: 100vh; overflow-y: hidden; scroll-snap-type: y mandatory; scroll-behavior: smooth; position: relative; } section { height: 100vh; display: flex; flex-direction: column; justify-content: center; align-items: center; scroll-snap-align: start; transition: background-color 0.5s ease; position: relative; background-size: 100% 100%; background-repeat: no-repeat; } .bg1 { background-image: url(&quot;./img/bg1.png&quot;); } .bg2 { background-image: url(&quot;./img/bg2.png&quot;); } .bg3 { background-image: url(&quot;./img/bg3.png&quot;); } .bg4 { background-image: url(&quot;./img/bg4.png&quot;); } .bg5 { background-image: url(&quot;./img/bg5.png&quot;); } .title { font-family: Alibaba-PuHuiTi, Alibaba-PuHuiTi; font-weight: 500; font-size: 80px; color: #e1e1e1; text-align: left; font-style: normal; text-transform: none; } .content { font-family: Adobe Heiti Std; font-size: 14px; color: #ffffff; line-height: 30px; text-align: justifyLeft; font-style: normal; text-transform: none; } .section-one { width: 100%; display: flex; justify-content: space-around; align-items: center; .content { width: 25%; } } .section-two { width: 30%; position: absolute; right: 150px; bottom: 180px; .content { margin-top: 60px; } } .section-three { width: 100%; display: flex; flex-wrap: nowrap; justify-content: space-around; align-items: center; .content { width: 600px; margin-top: 50px; } } .section-four { width: 100%; display: flex; justify-content: space-around; .title { font-size: 100px; font-weight: 700; margin-top: 100px; } .content { width: 600px; margin-top: 50px; } } .section-five { width: 100%; display: flex; justify-content: space-around; position: relative; .title { position: relative; margin-top: 500px; } .content { position: relative; margin-bottom: 200px; width: 600px; } } .nav-dots { position: fixed; right:40px; top: 50%; transform: translateY(-50%); display: flex; flex-direction: column; gap: 16px; z-index: 1000; } .dot { width: 8px; height: 8px; background-color: rgba(255, 255, 255, 0.6); border-radius: 50%; cursor: pointer; transition: background-color 0.3s ease; } .dot.active { background-color: #363636; transform: scale(1.7) } @keyframes bounce { 0%, 20%, 50%, 80%, 100% { transform: translateY(0); } 40% { transform: translateY(-20px); } 60% { transform: translateY(-10px); } } /* 最后一个区块不显示指示器 */ section:last-child .scroll-indicator { display: none; } /* 响应式设计 */ @media (max-width: 768px) { h1 { font-size: 2rem; } p { font-size: 1.2rem; } } &lt;/style&gt; 实现图片懒加载
07-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值