5 Replies - 300 Views - Last Post: 06 December 2017 - 12:28 PM Rate Topic: ***-- 2 Votes

#1 Rixterz  Icon User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 149
  • Joined: 26-August 12

A few beginner questions for C++

Posted 05 December 2017 - 02:58 AM

Hello,

I'm new to C++ so, having come from a C#/VB/Java background, there's a steep learning curve and some things don't make sense until they're explained. I have a few questions below which I'd appreciate some advice on, as I haven't found any reasonable explanations on the internet.

Firstly, 'const'.

I see it used a lot, and it's used to specify that a variable cannot have its value changed. This is useful for things which must not change, for example pi. However, if a variable doesn't need to be changed, is it absolutely necessary to declare it as const? What are the benefits of using/not using it?

Secondly, I'd like to make sure my understanding of pointers is correct. If so, all of the following should be valid:

SomeClass* instance = new SomeClass();
instance->doMethod();

int i = 2;
int* i_address = &i;
int i_again = *i_address;
int data_next_in_memory = *i_address++; // increase the memory location and get the value at it, ignore type as it could be anything.



Relating to 'const' again, if the pointer itself does not need to be modified (most situations), is it necessary to declare it as const? I believe to do this, the correct syntax would be:

int* const addr;

const int* const i = 9; // not sure if this syntax is correct, but it should make both the pointer and value immutable.



Thanks

This post has been edited by Rixterz: 05 December 2017 - 03:29 AM


Is This A Good Question/Topic? 0
  • +

Replies To: A few beginner questions for C++

#2 GazinAtCode  Icon User is offline

  • D.I.C Head

Reputation: 32
  • View blog
  • Posts: 114
  • Joined: 26-September 16

Re: A few beginner questions for C++

Posted 05 December 2017 - 03:56 AM

If you know it won't change, then declare it as const. This is particularly important in the context of stack-allocated memory. Compilers like VC++ have to know the size of each stack-allocated array at compile time, so removing the const keyword from the following code will make them complain:
const int n = 10;
int test[n];
I believe that neither int* const addr; nor const int* const i = 9; will work with any compiler. Maybe you meant someting like const int* const i = &n;, where n is a previously defined variable and &n is its address.

Making things constant may be useful in function signatures when you want to ensure that a function won't modify the actual values behind the pointer.

This post has been edited by GazinAtCode: 05 December 2017 - 11:26 PM

Was This Post Helpful? 1
  • +
  • -

#3 Rixterz  Icon User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 149
  • Joined: 26-August 12

Re: A few beginner questions for C++

Posted 05 December 2017 - 05:48 AM

Ok thanks for clarifying that, so let's say I have the following function, are my syntax and assumptions correct?:

I'm using 'const Something* const thingPointer' in this example because the intent of the function is to simply analyse the object and neither modify it nor its pointer.

Something* mySomething = new Something();
mySomething->setX(5);
mySomething->setY(30);
validate(mySomething);

bool validate(const Something* const thingPointer)
{
    thingPointer->x = 4; // should fail because of 'const Something*' making the Something instance immutable
    thingPointer++; // should fail because of 'Something* const' making the pointer to the Something instance immutable

    int myX = thingPointer->x; // should work fine
    int myY = thingPointer->y;
}


This post has been edited by Rixterz: 05 December 2017 - 05:59 AM

Was This Post Helpful? 0
  • +
  • -

#4 GazinAtCode  Icon User is offline

  • D.I.C Head

Reputation: 32
  • View blog
  • Posts: 114
  • Joined: 26-September 16

Re: A few beginner questions for C++

Posted 05 December 2017 - 11:16 PM

Yes, that looks correct to me (maybe except the missing return statement).

const TypeName* instanceName—you can't change the actual values the pointer points to (modify the object).
TypeName* const instanceName—you can't modify the pointer (address) itself (increment, decrement, etc.).
const TypeName* const instanceName—you can do neither of those things.

This post has been edited by GazinAtCode: 05 December 2017 - 11:27 PM

Was This Post Helpful? 1
  • +
  • -

#5 Rixterz  Icon User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 149
  • Joined: 26-August 12

Re: A few beginner questions for C++

Posted 06 December 2017 - 02:11 AM

Perfect. Thanks very much.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5955
  • View blog
  • Posts: 20,408
  • Joined: 05-May 12

Re: A few beginner questions for C++

Posted 06 December 2017 - 12:28 PM

Often, you don't need this:
bool validate(const Something* const thingPointer)



It'll be more common to see this:
bool validate(const Something* thingPointer)



This is because C and C++ pass by value parameter passing. Even if you had code that looked like this:
bool validate(const Something* thingPointer)
{
    :
    thingPointer++;
    :
}


The asserts will still be true:
Something* mySomething = new Something();
mySomething->setX(5);
mySomething->setY(30);
Something* before = mySomething;
validate(mySomething);
assert(before == mySomething);



Personally, the only reason why I would use the atypical constant pointer to a constant object is when I want to setup a safety net for myself (or future editors of my code) to not change the pointer within the scope of the function due to some weird logic I may have written. Normally, though, when I do something out of the norm, not only do I setup safety nets, but I also comment the crap out of it explaining why I doing something different from normal.

Some very good reading here: Const Correctness. Good correct information there, albeit a bit dry.

If you are really serious about your transition from C# to C++, I highly recommend Effective C++, More Effective C++, and Scott Meyers' other books.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1