POV-Ray : Newsgroups : povray.off-topic : My first C++ program : Re: My first C++ program Server Time
30 Sep 2024 19:30:50 EDT (-0400)
  Re: My first C++ program  
From: Invisible
Date: 19 Sep 2008 11:10:50
Message: <48d3c0fa$1@news.povray.org>
>> Ooo... I didn't realise you could do that. (Pass a const reference, that 
>> is.) So that means, what? If you accidentally try to change it, you'll 
>> get a compiler warning?
> 
>   No, you get a compiler error.

Even better.

>   Declaring everything you don't intend to modify as 'const' is a good
> programming practice in C++ for many reasons:
> 
> 1) If you don't intend to modify the parameter, very especially if the
>    parameter is a reference (in which case you would be modifying the
>    original rather than a copy), then you will get a compiler error if
>    you try to modify it anyways. This will catch programming mistakes
>    at compile time, which is a good thing.
> 
> 2) Declaring a reference parameter as 'const' is self-documenting: You are
>    not only telling the compiler that that parameter is not modified in the
>    function, more importantly you are telling that to a human reader. In
>    other words, you are effectively documenting that the parameter is a
>    pure input parameter, not something the function uses to pass values
>    to the calling code.
> 
> 3) As a bonus, it is possible that in certain situations the compiler might
>    be able to optimize const variables better than non-const ones.

Agreed.

>   Note that (not surprisingly) pointers are slightly more complicated
> with regard to 'const':
> 
>     const Type* ptr = something;
> 
>   This defines a pointer named 'ptr' which points to a const value.
> In other words, you can't modify the value pointed by 'ptr'. However,
> you can modify 'ptr' itself, to make it point somewhere else (or make
> it null, of whatever).
> 
>   If what you want is for 'ptr' itself to be const, not the value it's
> pointing to, you have to write it like this:
> 
>     Type* const ptr = something;
> 
>   Now you can modify the value pointed by 'ptr' but you can't modify 'ptr'
> itself. Naturally if you want *both* things, you write:
> 
>     const Type* const ptr = something;
> 
>   Now you can't modify either 'ptr' nor the value it's pointing to.
> 
>   References do not have this kind of complication because the reference
> variable itself is always const. What the 'const' keyword determines is
> whether the object it's pointing to is const or not.

...ouch. >_< OK, well let's hope I don't need to use pointers too much then!

>> Since you're here... do you happen to know the C++ name for stderr?
> 
>   std::cerr << "An error message.\n";

OK.

>   The problem with the >> operator is that you have to know in advance
> what the type of the input will be. In this case you don't know it because,
> as you say, the input can be an operator (+ or -) or a number.

Indeed.

>   What you can do is this:
> 
>     std::string s;
>     while(true)
>     {
>         std::cin >> s;
>         if(!std::cin.good()) break;
> 
>         int value;
>         std::istringstream iss(s);
>         iss >> value;
>         if(!iss.fail())
>             std::cout << "The input was an integer: " << value << "\n";
>         else
>             std::cout << "The input was something else: " << s << "\n";
>     }

...you mean, you can take a string and make a stream that will read from 
it? (Or, presumably, write to it, if you desire.) So what exactly do the 
good() and fail() functions do?

>>>> PS. Nano is a horrid, horrid text editor!
>>>   Why don't you use something better?
> 
>> Because my virtual machine is slow enough already without starting X11, 
>> and Nano is the only text-mode editor I have available (other than Vim 
>> and Emacs anyway).
> 
>   Why don't you use an editor in your host OS?

Because I haven't found a way to transfer files between the host OS and 
the guest OS.


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.