Const指针与类

类的成员函数在调用的时候,会有一个隐式的this指针,默认情况下,这个指针的类型是 ClassName * const this,这意味着this指针不能改变,但this指针指向的对象是可以改变的。

但如果成员函数不会改变对象本身,那么this指针应该被设计为const ClassName * const this,即this指针是一个指向常量的常量指针,其指向的对象不可变更,本身也不可变更。

但因为this是隐式的指针,那么当我们想要声明this指针指向的对象是常量时,我们在哪里声明呢?

就在成员函数的参数后面,加一个const。这就叫常量成员函数。

注意,常量的对象无法调用自身的非常量成员函数,原因就在于,非常量成员函数的this指针是:ClassName * const this,无法指向一个const的对象。

Const指针与类》有51个想法

  1. Emilio

    Could I borrow your phone, please? write essay cheap The Reserve Bank of India is due to review monetary policyon Oct. 29, with a rising pace of inflation bolstering odds foranother central bank interest rate hike even as the economystumbles through its worst crisis since 1991.

  2. Emmett

    Have you got any experience? buy viagra 100mg “I think George Zimmerman is a man whose heart was in the right place, but just got displaced by the vandalism in the neighborhoods, and wanting to catch these people so badly, that he went above and beyond what he really should have done,” the woman said, speaking from behind a shadow to hide her identity.

  3. Benny

    Do you know the address? write essay fast
    Lending isn’t willy-nilly: borrowers are cherry-picked by credit checks, and rated according to risk. These websites do all the repayment chasing on your behalf – so there’s none of the legwork you’d have if you were lending cash to a fella down the pub.

  4. Jaime

    I work for myself cheap viagra online
    Critics point to the disclosure of diplomatic cables and say that by disclosing confidential contacts between US embassies and individuals living under sometimes repressive regimes, he placed individuals into positions of danger, perhaps forcing some into hiding. Critics say it also had a wider chilling effect on people's willingness to talk to US officials. They say confidential contacts are a necessary part of diplomacy.

  5. Noble

    very best job cheap cialis online Despite the crucial international aid to avoid default and a supplementary write-down of its debt load by some 100 billion euros last year, at the end of the first half of 2013 Greece still had a 321 billion euro debt, according to the latest official data, which is considered unsustainable by some analysts.

  6. Shelby

    I came here to study buy cialis 10mg
    The new tablet comes with a $30 price increase over last year’s model. At $229 for the base model, it is still a bargain — and 30 percent cheaper than Apple’s $329 iPad Mini. The display is sharper and the sound is richer than the old model. There’s now a rear camera for taking snapshots. The new Nexus 7 is the first device to ship with Android 4.3, which lets you create profiles to limit what your curious and nosy kids can do on your tablet when you’re not around.

  7. Freddy

    The line’s engaged write essay cheap As late as this week, Oregon also had trouble correctlydisplaying information about insurance plans on a test site. Theproblem could mislead customers about deductibles, prices andother details if it occurs on the live site Tuesday.

  8. Lioncool

    Until August buy cialis 10mg
    South of Safira, at least 20 Syrian troops and seven rebel fighters were killed in a dawn attack by rebels on an air defense base. There have been regular clashes in the region in recent weeks as Assad’s forces have sought to open up a supply route from the south to Aleppo, bypassing the main north-south highway which is partly under rebel control.

  9. Sydney

    I stay at home and look after the children cheap viagra online
    Kenya has called for the cases to be dropped or moved closer to home. It has also rallied African support against the ICC, which has until now charged only Africans. The arrest warrant will test Kenya’s stated commitment to cooperate with the court.

  10. Derrick

    How long are you planning to stay here? buy viagra 100mg “We will not protect anyone who commits a crime involving public funds, which are sacred because they are the people's money. There are no untouchables here,” Francisco Ameliach, the governor of Carabobo state and another member of the ruling party, told state media.

  11. Franklin

    It’s a bad line write essay fast
    Illnesses associated with eating the Costco chicken are part of a larger outbreak tied to three Foster Farms production sites in Fresno and Livingston, Calif. USDA officials issued a public health alert for raw chicken made by the firms this month and threatened to close the sites by withholding inspections. But Foster Farms agreed to fix the problems that led to the contamination and federal officials agreed to allow the sites to remain open. 

  12. Harland

    A pension scheme cheap viagra online
    The 44 children rotting in Israel's jails are not on the list of prisoners the Israeli cabinet signed off on releasing in exchange for Abbas and his cronies agreeing to come back to the infamous negotiating table. The jailbirds set to be freed are not your average Palestinian prisoners and have all been doing time since before the signing of the Oslo Accords, by Arafat and Rabin, in 1993. These men are not being indefinitely detained without charges and were convicted by Israel of far worse things than tossing rocks at IDF tanks. This little detail helps to bolster Israel's PR machine by reenforcing the myth that all Palestinians are terrorists and Israel is putting her very existence at risk by agreeing to sit with these monsters again. Netanyahu also gets to drone on about how poor Israel got nothing in return for agreeing to reengage. He conveniently ignores the fact that billions of U.S. tax dollars continuously flow Israel's way and  America has, yet again, turned a blind eye and a deaf ear to the sights and sounds of Israeli bulldozers expanding the illegal settlements as Kerry, Livni and Erekat speak.

  13. Zachery

    very best job buy cialis 10mg
    Danoff, 53, has run New Insights for a decade. He is bestknown for his 23-year run at Contrafund. Over the past 20 years,Contrafund is No. 1 out of 53 large-cap growth funds withannualized returns of 10.33 percent, as of Aug. 31, according toLipper Inc data.

  14. Cliff

    I’m on business cheap viagra online
    Almunia said it would be much easier for a user to see competing services in Google search results irrespective of which device was being used. Rival firms had worried that even if concerns around computers were addressed, search results displayed on smaller devices such as mobiles could still prevent them from appearing prominently.

  15. Lawerence

    History cheap cialis online After three years in the U.S. Army, he served with the Chicago Police Department for 18 years, both as a uniformed officer (he was there for the 1968 Chicago riots) and a burglary detective, before he found his way into acting as he neared his forties.

  16. Tommy

    How much is a First Class stamp? cheap viagra online
    The infighting has undermined the rebel military campaign against President Bashar al-Assad. Their uprising began as peaceful protests against four decades of Assad family rule but degenerated into a war that has killed more than 100,000 people.

  17. Fausto

    No, I’m not particularly sporty write essay cheap When firefighters got to the scene, “we opened the front door and could see the fire in the back,” fire Chief John Bley said. It was unclear what caused the explosion on the first floor, he said, and there was a minor collapse inside the building but nothing structural.

  18. Gracie

    A jiffy bag write essay cheap Smithfield said on Wednesday that the U.S. government hasdecided to take an additional 45 days to review the planneddeal. Smithfield and Shuanghui submitted their proposal in Juneto the Committee on Foreign Investment in the United States, orCFIUS, an executive branch panel that examines foreigninvestment for potential threats to national security.

  19. Domenic

    Can I call you back? write essay fast
    According to ComingSoon.net, Larry Charles said that the pilot, “was really good, but … I think it was too hard-core for HBO. Also, HBO’s management shifted. HBO doesn’t have a good track record when it comes to black shows, and I felt like that may have had something to do with it also.”

  20. Gavin

    How many would you like? cheap cialis online Environment Canada said some parts of the city had been drenched with more than 100 millimetres of rain in Monday evening’s storm, easily beating the previous one-day rainfall record of 36 millimetres in 2008.

  21. Issac

    A financial advisor buy cialis 10mg
    A spokesman for the Food Standards Agency says in an emailed statement: “Independent experts from the Committee on Toxicity recently reviewed aluminium in the infant diet, including the levels of aluminium present in infant formula and taking into account the water used in reconstitution.

  22. Ervin

    A financial advisor write essay fast
    They also asked for the names of the key executives at Smithfield Foods involved in the manufacturing and marketing of heparin products, including pig intestines and crude heparin, and details of key managers at any Smithfield facility involved in manufacturing crude heparin.

  23. Carlo

    We went to university together buy cialis 10mg
    If the Court of Cassation rejects Berlusconi’s appeal he could serve only one year of his sentence due to a 2006 amnesty law, and at the age of 76 he would likely be granted house arrest, but he challenged judges to put him behind bars.

  24. Vicente

    I love the theatre cheap viagra online
    Deutsche Boerse rival The London Stock Exchange last weakbeat expectations for its fiscal first quarter revenues, helpedby acquisitions as well as improving sentiment which has helpedmarket volumes.

  25. Oliver

    Cool site goodluck 🙂 cheap cialis online “Just getting selected for the All-Star Game was a real feather in your hat because in those days, the players voted, so it wasn’t a popularity contest,” Koosman adds. “It was all that I thought it would be and then to enter the game with one swing of the bat on the line, winning or lose, you feel like you’re walking on air.”

  26. Dominick

    Best Site good looking buy viagra 100mg This is what you’d expect at media day from a team with high-priced veterans, a collection of winners who said all the right things Monday —they’ll lead by example, they’ll sacrifice their shots and stats, they’re removing all egos from a locker room filled with All-Stars.