Jump to content
  • Advertisement
Sign in to follow this  
ms75214

"const"

This topic is 2496 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

If someone asks you in an interview, what are all the different ways you can use "const" in C++, what would you say?

Share this post


Link to post
Share on other sites
Advertisement
const to qualify a variable, mentioning
- use with pointers ([font=courier new,courier,monospace]const T*[/font] vs. [font=courier new,courier,monospace]T* const [/font]vs. [font=courier new,courier,monospace]const T* const[/font])
- const references ([font=courier new,courier,monospace]int x = 5; const int& y = x;[/font])
- implicit internal and external linkage on globals

const to qualify a function return type
- implicit with built-in types

const to qualify a non-mutating member function ([font=courier new,courier,monospace]class Foo { size_t size() const; };[/font])
- mention 'mutable'

And then mention that my answer pertained to C++03 only. (I don't know if anything relevant changed in C++11.)

... and that I've probably forgotten something.

Share this post


Link to post
Share on other sites


const int* const someMethod(const int* const&) const;


Gotta love C++ biggrin.png

A function taking a reference to a constant pointer to a constant integer that returns a constant pointer to a constant integer.

Nothing hard about that.

When reading type names go from right to left, including the CV-qualifiers.
int const* **const*const*
is simply a pointer to a constant pointer to a constant pointer to a pointer to a pointer to a constant integer.

Share this post


Link to post
Share on other sites
Of course, there are also some exceptions when const matters. For example, what's the difference between these two function declarations?

void foo(int i);
void foo(const int i);

Share this post


Link to post
Share on other sites

int const* **const*const*
is simply a pointer to a constant pointer to a constant pointer to a pointer to a pointer to a constant integer.

Now *that* is wicked. Didn't even realize you could do stuff like that.

@SiCrane: No clue.

Share this post


Link to post
Share on other sites
There is no difference. C++ strips const off of function parameters in function declarations.You can declare a function without const on the parameters and define it with the const, and so on.

Share this post


Link to post
Share on other sites

Of course, there are also some exceptions when const matters. For example, what's the difference between these two function declarations?

void foo(int i);
void foo(const int i);


According to the standard? There is none. They are equivalent (per 8.3.5 - 3).

[quote name='Washu' timestamp='1329083433' post='4912355']
int const* **const*const*
is simply a pointer to a constant pointer to a constant pointer to a pointer to a pointer to a constant integer.

Now *that* is wicked. Didn't even realize you could do stuff like that.
[/quote]
Just because you CAN do stuff like that doesn't mean you SHOULD do stuff like that. The example was more there to illustrate how to read a deceleration to determine its type.

It should be noted that const int and int const are the same thing.

Share this post


Link to post
Share on other sites

There is no difference. C++ strips const off of function parameters in function declarations.You can declare a function without const on the parameters and define it with the const, and so on.

I didn't think so, but the way you worded it I figured there was a deep, obscure part of the standard you knew about that explained a careful difference between the two. I guess I'm just so used to you blowing my mind with C++ facts that I was expecting something new here.


Just because you CAN do stuff like that doesn't mean you SHOULD do stuff like that. The example was more there to illustrate how to read a deceleration to determine its type.

Of course. If anyone did that in real code I'd... I don't know what I'd do. I'd probably nuke the code base and start from scratch, seeing as they're clearly mad.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!